I am trying to implement a single-writer, multiple-reader queue in pthreads. The synchronization pattern works but eventuallly deadlocks after repeated requests (I believe). It works with one writer boss thread and one reader worker thread indefinitely, but if I have one writer boss thread, and multiple reader worker threads, it eventually hangs. When I backtrace in gdb, I see this:
// Boss:
Thread 1 (Thread 0x7ffff7fd1780 (LWP 21029)):
#0 0x00007ffff7bc44b0 in futex_wait
...
// Worker:
Thread 2 (Thread 0x7ffff42ff700 (LWP 21033)):
#0 0x00007ffff7bc39f3 in futex_wait_cancelable
...
// Worker:
Thread 3 (Thread 0x7ffff3afe700 (LWP 21034)):
#0 0x00007ffff7bc39f3 in futex_wait_cancelable
...
To me this seems like the workers are waiting on the signal, and the boss is hanging on the signal and not sending it. But, I don't know why that would happen.
I have tried this synchronization pattern:
// Boss:
pthread_mutex_lock(&queue_mutex);
queue_push(&queue, data);
pthread_cond_signal(&queue_condition);
pthread_mutex_unlock(&queue_mutex);
return;
// Worker(s):
pthread_mutex_lock(&queue_mutex);
while((queue_isempty(&queue)) > 0) {
pthread_cond_wait(&queue_condition, &queue_mutex);
}
data_t *data = queue_pop(&queue);
pthread_mutex_unlock(&queue_mutex);
do_work(data);
To the best of my knowledge, this is the correct synchronization pattern. But, evidence suggests I am not applying the correct pattern. Could someone help me understand why this single-writer, multiple-reader queue access in pthreads would not work as I intend?