Game Development Community

Thread Blocking

by Steve Bilton · in Torque Game Engine · 01/25/2008 (9:49 am) · 1 replies

Here I am again ...

If I have a thread running, can I block it until a certain parameter is met rather than constantly polling? I'd like to send a thread to sleep then later awaken it with an interrupt. Is there any functionality for this existing in TGE?

#1
01/30/2008 (6:26 pm)
Hrm. Well, you could implement such a thing using the existing winThread.cc code.

Windows will let you signal a blocked thread (as will Linux pthreads, etc.).

I replaced the winThread.cc code with a cross-platform thread.cc that uses winPthreads on Windows, which seems to work pretty well. That allowed me to create a "Condition" class that does exactly as you describe.

Here's a code snippet that uses it:

if (mNumConnections == 0)
      {
         Con::debugf(ConsoleLogEntry::Communication, "waiting on active connections");
         mHaveConnections->wait(mMutex, haveActiveConnections);
         mMutex->unlock();
         Platform::sleep(50);
      }

mHaveConnections is a pointer to a Condition object. 'wait' blocks this thread until the condition is met.
(You can look at 'pthread_cond_wait' to get an idea of the implementation of it... wait ultimately calls 'pthread_cond_wait').

The condition here is 'haveActiveConnections', which is actually a function, which returns true when there are connections.