I have written a small class for synchronizing threads of both Linux (actually Android) and Windows.
Here is the Win32 implementation of my interface :
The pthreads equivalent of your code is:
class SyncObjectPosix
{
private:
bool signalled;
pthread_mutex_t mutex;
pthread_cond_t cond;
public:
SyncObjectPosix()
{
signalled = false;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
}
~SyncObjectPosix()
{
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
void WaitForSignal()
{
pthread_mutex_lock(&mutex);
while (!signalled)
{
pthread_cond_wait(&cond, &mutex);
}
signalled = false;
pthread_mutex_unlock(&mutex);
}
void Signal()
{
pthread_mutex_lock(&mutex);
signalled = true;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
};