I have the a minimal example, which works as expected on Linux, but fails on SerenityOS. The code is below. Changing pthread_cond_broadcast to two calls (one for each thread) to pthread_cond_signal works as expected. I also tried locking the mutex while calling pthread_cond_broadcast however it made no difference.
Sorry in advance if I'm doing anything stupid here!
I'm running latest master, on x86_64
The pthread_cond_broadcast call wakes both waiting threads
Linux:
$ ./test
Waiting 5s for stuff to get going
Thread with TID 816330304 starting
Thread 816330304 waiting for cond
Thread with TID 807937600 starting
Thread 807937600 waiting for cond
Broadcasting condition variable
Joining threads
Thread 816330304 done waiting for cond
Thread 816330304 exiting
Thread 807937600 done waiting for cond
Thread 807937600 exiting
The pthread_cond_broadcast call wakes only one thread
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
struct ThreadData
{
pthread_mutex_t mtx;
pthread_cond_t cond;
};
void* thread_worker(void* arg);
void* thread_worker(void* arg)
{
printf("Thread with TID %d starting\n", pthread_self());
ThreadData* td = (ThreadData*)arg;
pthread_mutex_lock(&td->mtx);
printf("Thread %d waiting for cond\n", pthread_self());
pthread_cond_wait(&td->cond, &td->mtx);
printf("Thread %d done waiting for cond\n", pthread_self());
pthread_mutex_unlock(&td->mtx);
printf("Thread %d exiting\n", pthread_self());
return nullptr;
}
int main(int, char**)
{
pthread_t threadA, threadB;
ThreadData td;
pthread_mutex_init(&td.mtx, NULL);
pthread_cond_init(&td.cond, NULL);
pthread_create(&threadA, NULL, thread_worker, &td);
pthread_create(&threadB, NULL, thread_worker, &td);
printf("Waiting 5s for stuff to get going\n");
usleep(5000*1000);
printf("Broadcasting condition variable\n");
pthread_cond_broadcast(&td.cond);
printf("Joining threads\n");
pthread_join(threadA, nullptr);
pthread_join(threadB, nullptr);
pthread_cond_destroy(&td.cond);
pthread_mutex_destroy(&td.mtx);
return 0;
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too