Advertisement

multithreaded programming, its uses

Started by April 18, 2000 11:02 PM
3 comments, last by jritts 24 years, 8 months ago
In a game where most of the system resources are occupied, is there any advantage to using other threads for certain processes? I''m interested in making mouse drawing and updating be controlled by another thread, but i''m having a problem where once the new thread is run, the game slows. I imagine I need to lower the cpu priority of the second thread, can anyone tell me how to do this? -James
I''m fluent in C++ and Java, know something of Perl, HTML, DirectDraw.CSE student at the University of California San Diego.
You could do this with SetThreadPriority(HANDLE hthread,int priority).

hthread is the handle for your thread and priority can be one of the following:

THREAD_PRIORITY_IDLE
THREAD_PRIORITY_LOWEST
THREAD_PRIORITY_BELOW_NORMAL
THREAD_PRIORITY_NORMAL
THREAD_PRIORITY_ABOVE_NORMAL
THREAD_PRIORITY_HIGHEST
THREAD_PRIORITY_TIME_CRITICAL

But I made the experience that multithreading costs time and isn''t very useful if your programm runs on a single-cpu system.

Visit our homepage: www.rarebyte.de.st

GA
Visit our homepage: www.rarebyte.de.stGA
Advertisement
Not necessarily, GA, it can be very useful in cases where the exectution of a single process should not influence the execution of another.
Threading is a ( pretty ) fail-safe mechanism to keep certain parts of your program running at a certain priority level, even if other parts get blocked or hang.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
thanks a lot for your answers
i''ll try that GA. i dont have any uses yet other than mouse drawing routines.

thanks!
I''m fluent in C++ and Java, know something of Perl, HTML, DirectDraw.CSE student at the University of California San Diego.
Actually, with the mouse scenario (if you are using DirectInput), I would raise the second thread''s priority level to THREAD_PRIORITY_ABOVENORMAL and keep the routine very simple:


DWORD ThreadProc(LPVOID lpParam)
{
while( !g_bTimeToQuit )
{
lpdidMouse->GetDeviceState() // or data? been a while

Sleep(0); // relinquish time to the main thread
}

return 0;
}



Always Sleep(0) when you raise the priority level. Remember, the main thread usually has GetMessage() for waiting, but the other threads must sleep to relinquish time to each other. If they don''t Sleep(0), it WILL slow your game down, because Windows will give your mouse thread the full CPU time of your main thread, which isn''t necessary. You need to tell Windows when to schedule your threads. Oh, and you need to tell Windows when to quit, too, so set a global variable (or an HEVENT, which is better) and wait for the thread to exit (call WaitForSingleObject() on its handle).

Incidentally, threads are an (relatively) extremely easy way to control how much time certain parts of your program get to execute. On SMP (sp?) which your program might run, those threads will be allocated to different CPUs (theoretically) and you will actually gain time over single-threading. That''s not a normal case, but you never know who might use your game.



- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement