Question

I've recently seen the light of EventWaitHandle's powerful behavior in C# and decided to move some functionality in a sister application to do the same. The only problem is that the sister app is written in C.

No big deal, I'm using pthreads, which have a pthread_cond_t datatype that allows for signalling. My only question is, is it possible for a cond to be 'signalled' before something is waiting on it?

Right now my testing says no. That is, if ThreadA fires a signal before ThreadB is waiting, ThreadB will wait indefinately. Is there another pthread type that I can use that behaves closer to the functionality of the EventWaitHandle in C#? An object is signalled, meaning that the first thread to wait on it, will pass immediately, and set it to unsignalled.

Wrapping the pthread_cond into another data structure wouldn't be too hard to achieve this. But again, is this functionality already available in the pthread library?

Was it helpful?

Solution

If you're using condition variables correctly, this won't matter.

The basic flow of your code should be (in pseudocode):

lock(lockobj);
while (!signalled) {
    wait(condvar);
}
signalled = false;
unlock(lockobj);

on the waiting side, and:

lock(lockobj);
signalled = true;
notify(condvar);
unlock(lockobj);

on the signalling side. (Of course, the lock object and condition variable used have to be the same on both sides.) Hope this helps!

OTHER TIPS

Alternative answer (also in pseudocode) if you want multiple signallings (i.e., if signalled twice, then two threads can wait before the state is unsignalled again).

Waiting side:

lock(lockobj);
while (signalled != 0) {
    wait(condvar);
}
--signalled;
unlock(lockobj);

Signalling side:

lock(lockobj);
++signalled;
notify(condvar);
unlock(lockobj);

I ended up just wrapping a condition type in a new structure and created some simple functions to behave much like the EventWaitHandle from C#. I needed two mutexes to acheive proper serialized access.

The the cond_mutex is used for waiting on the conditional variable, while the data_mutex is used when setting the state from signaled to not signaled.

The reset mode is the same from C#. AUTO or MANUAL. This allows for the event_wait_t to reset itself automatically after waiting. Or letting the programmer manually do it with a call to event_wait_reset(event_wait_t *ewh);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top