You need a mutex, a condition variable, and an auxiliary variable.
in stream 1:
pthread_mutex_lock(&mtx); // We wait for helper to change (which is the true indication we are // ready) and use a condition variable so we can do this efficiently. while (helper == 0) { pthread_cond_wait(&cv, &mtx); } pthread_mutex_unlock(&mtx);
in stream 2:
pthread_mutex_lock(&mtx); helper = 1; pthread_cond_signal(&cv); pthread_mutex_unlock(&mtx);
The reason you need an auxiliary variable is because condition variables can suffer from false awakening . It is a combination of an auxiliary variable and a condition variable that gives you exact semantics and effective expectation.
source share