I have 3 processes which need to be synchronized. Process one does something then wakes process two and sleeps, which does something then wakes process three and sleeps, which d
I have no clue on what is going wrong and the code looks fine too me. Here are some things you could do do somehow get more information.
As pointed by Jens, there are two races :
The first is when evaluationg the value of the semaphore, after the call to sem_timedwait. This is not changing the control flow whit respects to semaphore. Wether the thread timedout or not, it still goes through the "should I trigger the next thread" block.
The second is in the "Should I wakeup the next thread" part. We could have the following events :
sem_getvalue(trigger)
and gets a 1sem_timedwait
and the semaphore goes to 0Now, I can't see how this could trigger the observed behaviour. After all, since Thread n+1 is waked up anyway, it will in turn wake up thread n+2 which will wake up thread n etc...
While it is possible to get glitches, I can't see how this could lead to systematic timeout from a thread.