BlitzMax Thread Priority

BlitzMax Forums/BlitzMax Programming/BlitzMax Thread Priority

Grey Alien(Posted 2006) [#1]
Does anyone know how to set the priority of a thread in code? With the help of a framework customer, I've found out that some background tasks can interefere with BlitzMax but if the priority of the thread is pushed up to "high", the interference stops. I'd really like the option to do this with code and wondered if anyone has any experience of this? Also does such code work in 98/Me or is it Win 2000/XP only I wonder?

thanks in advance...


FlameDuck(Posted 2006) [#2]
With the help of a framework customer, I've found out that some background tasks can interefere with BlitzMax
Which ones? If your BlitzMAX application is runing at 100% CPU utilization, you only have one logical processor, and you set the thread priority to high, you can say goodbye to windows too.

I seriously doubt the validity of this claim, and if it is valid it's most certainly a BlitzMAX bug that needs fixing ASAP. It's certainly not something you'd want to potentially freeze up a customers PC with.


Robert Cummings(Posted 2006) [#3]
I get the same problem.

Even small things cause my game to stutter as if its priority was zero.


Grey Alien(Posted 2006) [#4]
Basically the guy was getting some jerks on my framework demos. He found a Yahoo Weather thing running. Disabled it and the demo ran fine. He re-enabled the weather thing and set the demo priority to high and the demo ran fine. I've been through the testing process with him.

Basically I don't want my games to run dodgily on some PCs with certain background tasks running (you can't expect end-users to disable background tasks). On my PC my demo works fine with loads of stuff running, but it seems that certain tasks hog just a bit too much CPU time on some PCs. As BlitzMax seems willing to give up this CPU time, I'm looking for a method into bullying it into hogging CPU time for better game performance on more PCs.


Grey Alien(Posted 2006) [#5]
OEJ: If you boost the priority in Task Manage/Processes, does this help?


FlameDuck(Posted 2006) [#6]
Basically I don't want my games to run dodgily on some PCs with certain background tasks running (you can't expect end-users to disable background tasks).
Sure you can.

Exactly what degree of CPU / Frametime utilization are we talking here?

Generally high priority is reserved for system critical tasks (like the tasksceduler) and jacking it up to high can have very undesired results, particularly if you're on a single logical processor, and are already pushing the systems performance envelope.


Dreamora(Posted 2006) [#7]
You do not really need to raise the priority (as mentioned above, a bad idea). But you need to use it in fullscreen in which case the framework will get "higher priority" ...

If you have it running in window mode, it is normal that it does not run fluently all if other CPU intense stuff hopps in (even if they only do it for 2s each 2 mins)... app windowed mode is meant for apps (=> don't need to have the same cpu time all the time) and not for games after all.


ImaginaryHuman(Posted 2006) [#8]
If you put up the priority too high you will probably lose updates to your event queue too ? Any GUi code will grind to a halt.

Unless you take over the whole system like in the Amiga days you can't be sure that you wont have interference, all you can do is allow for it, aim for less than 100% cpu time and know that you're doing so, so you let the o/s do what it wants to do when it wants to do it. You have to cooperate with it.

Also what are you going to do if someone is running some other CPU-hogging app on their system?


Grey Alien(Posted 2006) [#9]
I realise that windowed mode should multi-task a bit more why is why I have delay(1) in the windowed mode main loop. However, yes in full-screen I want other tasks not to be able to interrupt. All the have to do is hog 20-50ms of time every second or so and suddenly the game looks jerky. This can be prevented by boosting the thread priority, or is there another way? In DOS, a game would get interrupted by nothing and run smooth, but run that same DOS game in windows and you get jerks, nice. I just want to run games in-fullscreen and have them axe all other threads while running, perhaps even axe most threads while the game is focused in windowed mode, but when unfocused, allow windows to have all the CPU time it wants. This sin't so much to ask from a game programming language is it?


Dreamora(Posted 2006) [#10]
If you run it in fullscreen and do not use delay, it should get all the CPU time (beside time that was granted to higher / Highest / realtime priority threads) ... if you add delay then you will have serious problems as delay 1 ms can take longer than 1 ms if a different thread is doing something at that point. This can become especially worse on systems with VSync on when you skip the "right" ms for flipping because it then will have influence on later flips as well (for that reason I do flip 0 time based)

I assume you used time difference based interpolation for your framework ... and in that case, these differences shouldn't mather that much ...


ozak(Posted 2006) [#11]
I agree. Do not mess with OS priorites!!!


Grey Alien(Posted 2006) [#12]
Dreamora: In full-screen there is no delay(1) but some people (not very many at all) are getting jerks due to background tasks, it's the same in windowed mode. Watching the timer, you can see it make sudden jumps as the game loses CPU time to other tasks, some taking up to 200MS! My framework can do Flip 0 or 1 (or -1), VSync is on the options page and in the ini file. VSync on/off makes no difference to the jerks people are seeing because it's due to the timer skipping ahead a lot due to background tasks. Yes I used time difference based interpolation.

I still want to mess with OS priorities.


ImaginaryHuman(Posted 2006) [#13]
Grey, if you hog the cpu in window mode you may find you have a very difficult time even getting the focus to switch to a different window because the GUI may not get any time to handle those events. Catch 22.

I think maybe it's a good thing to try and get your framework as smooth as possible and to deal with other threads interrupting, but I wouldn't lose too much sleep over it. How do you know that the user doesn't WANT there to be other threads running or other apps in the background. Maybe they're playing a game while doing rendering. If they are choosing to put software on their system that takes up CPU time, that's their business, and you can only do so much to prevent it.


Grey Alien(Posted 2006) [#14]
well I'm really thinking of the causal gamer with a spyware infested PC who plays a BlitzMax game and goes "it's all jerky" and doesn't buy it. If the performance can be increased on such PCs, then cool.


Tibit(Posted 2006) [#15]
Would it work to only use delay if the logic framerate allows so? If lag, then do not do delay?


Grisu(Posted 2006) [#16]
If you use bmx gui with a proper framework there is no need for a delay!


Liquid(Posted 2006) [#17]
It was me that reported this situation to Grey.

When the framework runs, in fullscreen, with the process priority in HIGH the animation/movement is smooth.
Is obvious that this setting should only be done in fullscreen mode. If you are playing a game in fullscreen you expect that the game gets all the speed it needs. I'm not talking about giving the process REALTIME speed. But HIGH can make a huge difference in the performance of the application.


TartanTangerine (was Indiepath)(Posted 2006) [#18]
Why talk about it, why not do something about it?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getcurrentthread.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setthreadpriority.asp

There ya go, that'll take you 2 minutes to implement.


Grey Alien(Posted 2006) [#19]
Wave: There are still jerks in full-screen mode wih no Delay(1). Same in windowed mode with Delay(1).

Grisu: I wanted people to be able to use a framwork that didn't require them to buy blitzMax. After all BlitzMax standalone is supposed to be for game isn't it?

Tim. Thanks. When did you have your procrastination gland removed?


FlameDuck(Posted 2006) [#20]
But HIGH can make a huge difference in the performance of the application.
True. For instance setting your application to HIGH priority will set it at the same priority as the Client Server Runtime Subsystem, and on a single CPU maching, may prevent it from being scheduled for execution (which may cause STOP errors).

I'm not sure most spyware infested PC users are going to want to trade a little jerkiness for a BSOD.


Liquid(Posted 2006) [#21]
Ok FlameDuck. Then give the application ABOVE NORMAL priority.
That should give extra processing power to the application without harming other tasks.