Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jritts

multithreaded programming, its uses

This topic is 6608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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**

Share this post


Link to post
Share on other sites
thanks a lot for your answers
i''ll try that GA. i dont have any uses yet other than mouse drawing routines.

thanks!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!