Skip to main content
GameDev.net gamedev.net
🔒 Locked

Native threads and semaphores

Started by Stowelly Mar 27, 2009 at 7:18 AM 2 replies 1.2k views
Original Post
Stowelly
Stowelly
Hi just wondering if anyone could give me some information about the right way (or a right way) to achieve this please? basically I have a native thread using the windows API calling an update function in my app. a flag is set by the main thread to tell this thread to update. the entry point to the thread looks something like this while(app_not_finished) { if(update_flag) { do stuff; } } and im setting the update flag (and all my other sync variables) between a semaphore aquire and release.... which i think is the equivilent of blocking the thread so the data can be safely modified??? but im a little unsure of the way im handling my entry point, as someone mentioned i should be using the semaphore to sleep / activate the thread instead of having the work carried out in a spin-lock type loop, and im not sure i understand the theory / process of this. would anyone be kind enough to explain what im trying to achieve please? thanks, i hope this isnt too vague
http://stowelly.co.uk/
elFarto
elFarto
In Java the process would be:

Object obj = new Object();volatile boolean doWork = false;volatile boolean appRunning = true;public void makeThreadDoWork(){  synchronized(obj)  {    doWork = true;    obj.notify();  }}public void run(){  while(appRunning)  {    while (!doWork)    {      synchronized(obj)      {        obj.wait();      }    }    doSomeWork();    doWork = false;  }}

The run method spends most of its time in the obj.wait() line. When you want it todo some work you set doWork to true then notify() the obj to cause the other thread to wake up.

You don't mention which language you're using so it's difficult to comment on specifics.

Regards
elFarto

[Edited by - elFarto on March 27, 2009 8:00:35 AM]
Jansic
Jansic
There are numerous ways of doing this; personally I'd send messages using PostThreadMessage and pick them up with GetMessage, but it depends on what your thread needs to be doing.

If you want to do it this way, bear in mind that it's much easier to control things using the synchronisation primitives rather than trying to corral threads into safely reading flags from memory.

Here's some _extremely_ crude Windows API pseudocode which uses a suspended thread to perform a task every time the semaphore is signalled and exit reasonably cleanly...

thread(){    while (true)    {       if ( WaitForMultipleObjects( hSemaphore, hExit ) == WAIT_OBJECT_0 )           update();       else           break;    }}main(){   hExit = CreateEvent();   hSemaphore = CreateSemaphore();   hThread = CreateThread();   // More stuff + update   do_stuff();   ReleaseSemaphore(hSemaphore);      // More stuff + update   do_more_stuff();   ReleaseSemaphore(hSemaphore);   // Exit   SetEvent(hExit);   WaitForSingleObject(hThread);}


Not my finest 10-minute lunchtime post, but there you go. Hope that helps.

Jansic
Stowelly
Stowelly
thanks guys, both replies where very helpfull!!!
http://stowelly.co.uk/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.