Suppose multiple producers and consumers share a common queue.
Consumers are signaled when any producer puts data to the queue.
1. Does the following code has race condition?
Code://Producer: pthread_mutex_lock(&lock); //put data to a queue pthread_cond_broadcast(&cond); pthread_mutex_unlock(&lock);
2. If I move the line pthread_cond_broadcast(&cond); out of mutex box as:Code://Consumer: pthread_cond_wait(&cond, &lock); //get data from the queue pthread_mutex_unlock(&lock);
Does it has the same effect as the above code snippet?Code://Producer: pthread_mutex_lock(&lock); //put data to a queue pthread_mutex_unlock(&lock); pthread_cond_broadcast(&cond);



LinkBack URL
About LinkBacks



CornedBee