My program performs "thread synchronization" using pthread_cond_wait and pthread_cond_signal. It seems to fail about one in 20 times.
I have a shared variable count which is initialized to 0.
One thread increments count until count is 20 , then signals a condition variable.
The code is below.
void* inc_count(void *parm)
{
int i;
for(i = 0 ; i <25; i ++)// adds count to 25
{
pthread_mutex_lock(&mutex1);
count ++;
printf("Thread %lld , count = %d\n",(long long int)pthread_self(),count);
if(count == 20)
{
pthread_cond_signal(&cond);
printf("Thread %lld sends a signal!\n",(long long int)pthread_self());
}
pthread_mutex_unlock(&mutex1);
}
pthread_exit(NULL);
}
void* watch_count(void *parm)
{
while(count < 20)
{
pthread_mutex_lock(&mutex1);
pthread_cond_wait(&cond,&mutex1);
printf("Thread %lld receives the signal!\n",(long long int)pthread_self());
}
pthread_mutex_unlock(&mutex1);
pthread_exit(NULL);
}
int main()
{
pthread_t pt1,pt2,pt3,pt4;
pthread_mutex_init(&mutex1,NULL);
pthread_mutex_init(&mutex2,NULL);
pthread_cond_init(&cond,NULL);
pthread_create(&pt1,NULL,inc_count,NULL);
pthread_create(&pt2,NULL,watch_count,NULL);
pthread_join(pt1,NULL);
pthread_join(pt2,NULL);
}
From the picture, you can see that thread pt2 didn't receive the signal, why?
Your watch_count() function has problems:
You're locking the mutex inside a while loop and only unlocking outside it (could try to lock more than once without unlocking)
You don't hold the mutex when checking count < 20
To fix it, you need lock the mutex before the loop instead of inside it:
pthread_mutex_lock(&mutex1);
while(count < 20)
{
pthread_cond_wait(&cond,&mutex1);
printf("Thread %lld receives the signal!\n",(long long int)pthread_self());
}
pthread_mutex_unlock(&mutex1);
That will keep you from locking the mutex more than once before unlocking, and ensure that count can't be modified and the signal can't be sent between the test in the while condition and the pthread_cond_wait (inc_count()'s change to count and sending the signal would have to occur either before the while loop, or during the pthread_cond_wait()).
Even with those changes, there's still the possibility that the wait won't happen at all if count is already 20 by the time watch_count() checks it, though.
#Dmitri's diagnosis at the end of his comment is the likely cultprit IMO: it's possible that your inc_count thread gets past 20 before the watch_count thread first checks the while condition.
Why use the while loop at all? When inc_count sends the signal, you already know that the count is over 20, so why check for it?
My solution is to do some handshaking and make sure that watch_count enters its wait condition before inc_count starts counting. To achieve this, you need to make sure that the following things happen in order:
inc_count enters its code first and then waits for watch_count to initialize
watch_count enters its code and signals inc_count that it's ready to go
watch_count waits for inc_count to signal it
inc_count starts counting and signals watch_count when it gets to 20
I achieved this by grabbing a mutex before starting either thread, then counting on the first thread to enter its "ready and waiting" condition before starting the second thread. When the second thread is ready it signals the first thread to go ahead and start counting, here's my code:
void* inc_count(void *parm)
{
// holding this mutex on entry;
pthread_mutex_t* m = parm;
int i;
// wait for the watch_count thread to be ready
pthread_cond_wait(&init_cond, m);
pthread_mutex_unlock(m);
// start counting
for(i = 0 ; i < 10000; i ++)
{
pthread_mutex_lock(&count_mutex);
count++;
printf("Thread %lld , count = %d\n",
(long long int) pthread_self(),
count);
if(count == 20)
{
pthread_cond_signal(&count_cond);
printf("Thread %lld sends a signal!\n",
(long long int)pthread_self());
}
pthread_mutex_unlock(&count_mutex);
}
pthread_exit(NULL);
}
void* watch_count(void *parm)
{
// holding this mutex on entry
pthread_mutex_t* m = parm;
pthread_cond_signal(&init_cond); // tell inc_count that you're ready
pthread_cond_wait(&count_cond,m); // wait for him to get to 20
pthread_mutex_unlock(m);
pthread_mutex_lock(&count_mutex);
printf("Thread %lld receives the signal! count == %d\n",
(long long int)pthread_self(),
count);
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}
int main()
{
pthread_t pt1,pt2;
pthread_mutex_t init_mutex, count_mutex;
pthread_mutex_init(&init_mutex,NULL);
pthread_mutex_init(&count_mutex,NULL);
pthread_cond_init(&init_cond,NULL);
pthread_cond_init(&count_cond,NULL);
// this mutex is released when inc_count has initialized
pthread_mutex_lock(&init_mutex);
pthread_create(&pt1,NULL,inc_count,(void *) &init_mutex);
// this mutex is released when watch-count has initialized
pthread_mutex_lock(&init_mutex);
pthread_create(&pt2,NULL,watch_count,(void *) &init_mutex);
pthread_join(pt1,NULL);
pthread_join(pt2,NULL);
}
Related
Hi i'm new to C and trying to understand mutex, condition and threads. I have the basics of how thread works. Correct me if i'm wrong, from what i understand here is that one thread is waiting on the other 2 thread to send a signal to wake him up. The code can only be run by one thread at a time because of the mutex, thread 2 acquires the lock and runs the code, the sleep(1) helps to alternate thread so thread 2 doesn't run it again, and so thread 3 acquires the lock and runs the code and so on. Only the thread that locks and release the mutex lock. Once it reaches the COUNT_LIMIT 12, it sends a signal to the waiting thread which is thread 1. I understand up till here if i didn't misunderstood anything
So my question is that this few pieces of code here that is in void *watch_count:
pthread_mutex_lock(&count_mutex);
pthread_cond_wait(&count_threshold_cv, &count_mutex);
pthread_mutex_unlock(&count_mutex);
Is it acquiring the same lock as the one in void *inc_count? If so it locks the mutex and the pthread_cond_wait is waiting for the signal in void *watch_count, then continues to execute the code? What is the pthread_mutex_lock and pthread_mutex_unlock doing in void *watch_count, does this play a part in stopping the threads 2 and 3 from executing while waiting for thread 1 to finish it's job?
I hope i am making sense or exaplaining what i understand correctly, thanks in advance for any advice given!
#define NUM_THREADS 3
#define TCOUNT 10
#define COUNT_LIMIT 12
int count = 0;
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;
void *inc_count(void *t)
{
int i;
long my_id = (long)t;
for (i=0; i < TCOUNT; i++) {
pthread_mutex_lock(&count_mutex);
count++;
/*
Check the value of count and signal waiting thread when condition is
reached. Note that this occurs while mutex is locked.
*/
if (count == COUNT_LIMIT) {
printf("inc_count(): thread %ld, count = %d Threshold reached. ",
my_id, count);
pthread_cond_signal(&count_threshold_cv);
printf("Just sent signal.\n");
}
printf("inc_count(): thread %ld, count = %d, unlocking mutex, i: %d\n",
my_id, count, i);
pthread_mutex_unlock(&count_mutex);
/* Do some work so threads can alternate on mutex lock */
sleep(1);
}
pthread_exit(NULL);
}
void *watch_count(void *t)
{
long my_id = (long)t;
printf("Starting watch_count(): thread %ld\n", my_id);
pthread_mutex_lock(&count_mutex);
while (count < COUNT_LIMIT) {
printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count);
pthread_cond_wait(&count_threshold_cv, &count_mutex);
printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count);
printf("watch_count(): thread %ld Updating the value of count...\n", my_id,count);
count += 125;
printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
}
printf("watch_count(): thread %ld Unlocking mutex.\n", my_id);
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int i, rc;
long t1=1, t2=2, t3=3;
pthread_t threads[3];
pthread_attr_t attr;
/* Initialize mutex and condition variable objects */
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init (&count_threshold_cv, NULL);
/* For portability, explicitly create threads in a joinable state */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[0], &attr, watch_count, (void *)t1);
pthread_create(&threads[1], &attr, inc_count, (void *)t2);
pthread_create(&threads[2], &attr, inc_count, (void *)t3);
/* Wait for all threads to complete */
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
NUM_THREADS, count);
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);
}
Yes, all your threads are acquiring the same lock (there is only one mutex in this program - count_mutex).
When watch_count() calls pthread_cond_wait(), it unlocks the mutex and is suspended in one atomic action. This allows other threads to acquire the lock. When the thread that called pthread_cond_wait() wakes up, it will re-acquire the lock before pthread_cond_wait() returns (this means that if another thread has the mutex locked, pthread_cond_wait() can't return until that other thread releases the lock first).
You must have the mutex locked when you call pthread_cond_wait() - this is a requirement of the API. Furthermore, the watch_count() thread locks the count_mutex so that it can access the count variable without introducing a race condition (a concurrent modification of count from another thread). Notice that none of the threads access the count variable - either for read or write - without having the count_mutex locked at the time.
How do I make sure that both Producer and Consumer functions run infinitely and one after another? For example: First, I want the user to enter the array and then the consumer function prints the entered array and then again asks the user to input the array.
/* Headers */
pthread_mutex_t mutex;
pthread_cond_t cond,cond1;
void *producer(void *arg);
void *consumer(void *arg);
static int n;
int consumerFlag[100];
void *producer(void *arg)
{
pthread_mutex_lock(&mutex);
while(1)
{
printf("\n Enter no of terms");
scanf("%d",&n);
int i;
printf("\n Enter consumer flag array");
for (i = 0; i < n; i++)
{
scanf(" %d",&consumerFlag[i]);
pthread_mutex_unlock(&mutex);
}
pthread_cond_signal(&cond);
usleep(1000);
}
}
void *consumer(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("\nConsumer Function");
while(1)
{
printf("\nConsumer thread waiting");
for (i = 0; i < n; i++)
{
printf("\nConsumerFlag %d = %d", i, consumerFlag[i]);
pthread_mutex_unlock(&mutex);
}
}
}
int main()
{
int i=0;
pthread_mutex_init(&mutex,0);
pthread_cond_init(&cond,0);
pthread_t pThread, cThread;
pthread_create(&pThread, 0, producer, 0);
pthread_create(&cThread, 0, consumer,0);
pthread_join(pThread,NULL);
pthread_join(cThread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
First, I want the user to enter the array and then the consumer function prints the entered array and then again asks the user to input the array.
There are few major issues in your code.
A mutex can be unlocked by a thread only if the thread has locked it before. In your code, mutex is locked outside loop function and unlocked inside the loop. It will give undefined behavior.
Consumer function is doing signal_wait() before entering loop. I think you have assumed that the producer and consumer threads start almost simultaneously and producer would not signal before consumer locks the mutex. This is wrong. You should assume that threads get scheduled in a random order and handle all possible cases.
You are not notifying producer when consumer has consumed data completely. There is no signalling mechanism from consumer to producer.
You are notifying consumer when producer has produced data completely. But consumer is not waiting outside while loop. It means, only once it may get notification.
In producer, the condition_signal is after unlocking mutex, which is wrong. Signal should be called with mutex locked.
So in producer, you need to produce data, send signal to consumer and wait for confirmation from consumer that data is consumed. You need to set a flag additionally to take the case where producer thread acquire lock before consumer thread into account. In this case, consumer would not have entered the while loop when producer sends signal. So consumer can not catch the signal.
void *producer(void *arg)
{
pthread_mutex_lock(&mutex);
while(1)
{
printf("\n Enter no of terms");
scanf("%d",&n);
int i;
printf("\n Enter consumer flag array");
for (i = 0; i < n; i++)
{
scanf(" %d",&consumerFlag[i]);
//pthread_mutex_unlock(&mutex); //Should not be unlocked in loop
}
data_produced_flag = 1; //A flag to indicate data is ready. Should be initialized to 0 before thread creation. It is required if producer produces before consumer thread goes to pthread_cond_wait()
pthread_cond_signal(&cond_producer); //Should send signal with mutex locked
//usleep(1000); //Not required as it is going to wait for signal from consumer
pthread_cond_wait(&cond_consumer, &mutex); //Wait for confirmation that data is consumed
}
pthread_mutex_unlock(&mutex);
}
In consumer function, you should check if the data is already produced. If not produced, wait for for signal from producer. When data is available, consume and signal producer to get fresh data.
void *consumer(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
//pthread_cond_wait(&cond, &mutex); //Wait inside loop
printf("\nConsumer Function");
while(1)
{
if(0 == data_produced_flag)
{
printf("\nConsumer thread waiting");
pthread_cond_wait(&cond_producer, &mutex); //Wait for signal from producer
}
//else
//{
//Data is already produced. No need to wait.
//}
//Data is available for consumption
for (i = 0; i < n; i++)
{
printf("\nConsumerFlag %d = %d", i, consumerFlag[i]);
//pthread_mutex_unlock(&mutex); //We are in the process of consumption. Don't unlock mutex.
}
data_produced_flag = 0; //Reset the flag. Ready for new data.
pthread_cond_signal(&cond_consumer); //Indicate that data is consumed
}
pthread_mutex_unlock(&mutex);
}
The thread calling pthread_cond_signal is re-grabbing the mutex before the signaled thread can be released.
The code below shows a simple example of the issue at hand. The main thread will hold the lock, create the worker thread, and then enter a loop that prints data as it comes in. It is signaled to run via a conditional variable.
The worker thread will enter a loop that generates data, grabs the lock, writes the data, and then signals the main thread.
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int data = 0;
void *thread(void *arg) {
int length = *(int *) arg;
for (int i = 0; i < length; i++) {
// Do some work
pthread_mutex_lock(&lock);
data = i;
fprintf(stdout, "Writing\n");
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
}
pthread_exit(0);
}
int main(int argc, const char *argv[]) {
pthread_t worker;
int length = 4;
pthread_mutex_lock(&lock);
pthread_create(&worker, 0, thread, &length);
for (int i = 0; i < length; i++) {
fprintf(stdout, "Waiting\n");
pthread_cond_wait(&cond, &lock);
fprintf(stdout, "read data: %d\n", data);
}
pthread_mutex_unlock(&lock);
pthread_join(worker, NULL);
return 0;
}
This will give the following output:
Waiting
Writing
Writing
Writing
Writing
read data: 3
Waiting
Expected behavior:
Main thread holds the mutex and only releases it once its waiting. The worker thread will then write its data and signal the main thread. The main thread will immediately lock the mutex on signal, then read the data and go back to waiting, releasing the mutex. Meanwhile the worker thread will do its work and be waiting until the main thread is waiting again to write its data and signal it.
Instead, it appears that the worker thread is getting the mutex immediately after calling signal, rarely letting the main thread get access. If I put a sleep in the worker thread in place of the // Do some work then it will give the expected output.
Signalling the condition variable does not give any kind of priority on locking the mutex to a thread that was waiting on that condition variable. All it means is that at least one thread waiting on the condition variable will start trying to acquire the mutex so it can return from the pthread_cond_wait(). The signalling thread will keep executing and can easily re-acquire the mutex first, as you've seen.
You should never have a condition variable without an actual condition over some shared state that you're waiting for - returning from a pthread_cond_wait() doesn't mean a thread should definitely proceed, it means that it should check if the condition it was waiting for is true. That's why they're called condition variables.
In this case, the state your writing thread wants to wait for is "the main thread has consumed the last data I wrote.". However, your reading (main) thread also needs to wait on a condition - "the writing thread has written some new data". You can achieve both these conditions with a flag variable that indicates that some new, unconsumed data has been written to the data variable. The flag starts out unset, is set by the writing thread when it updates data, and is unset by the main thread when it reads from data. The writing thread waits for the flag to be unset, and the reading thread waits for the flag to be set.
With this arrangement, you also don't need to have the mutex locked when you start the writing thread - it doesn't matter which order the threads start, because everything is consistent either way.
The updated code looks like:
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int data = 0;
int data_available = 0;
void *thread(void *arg)
{
int length = *(int *) arg;
for (int i = 0; i < length; i++) {
// Do some work
pthread_mutex_lock(&lock);
fprintf(stdout, "Waiting to write\n");
while (data_available)
pthread_cond_wait(&cond, &lock);
fprintf(stdout, "Writing\n");
data = i;
data_available = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
}
pthread_exit(0);
}
int main(int argc, const char *argv[])
{
pthread_t worker;
int length = 4;
pthread_create(&worker, 0, thread, &length);
for (int i = 0; i < length; i++) {
pthread_mutex_lock(&lock);
fprintf(stdout, "Waiting to read\n");
while (!data_available)
pthread_cond_wait(&cond, &lock);
fprintf(stdout, "read data: %d\n", data);
data_available = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
}
pthread_join(worker, NULL);
return 0;
}
Of course, the threads end up working in lockstep - but essentially you have a producer-consumer with a maximum queue length of 1, so that's expected.
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg);
void *consumer(void *arg);
int buffer[100];
static int n;
void *producer(void *arg) { //`For taking user input`
int i;
printf("\n Enter the Array of %d terms",n);
for (i = 0; i < n; i++)
{
pthread_mutex_lock(&mutex);
scanf(" %d\n",&buffer[i]);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
void *consumer(void *arg) { // For printing the input array
int i;
printf("\nConsumer Function");
pthread_mutex_lock(&mutex);
for (i = 0; i < n; i++)
{
printf("%d\n",buffer[i]);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
int i=0;
pthread_mutex_init(&mutex, 0);
pthread_cond_init(&cond, 0);
pthread_t pThread, cThread;
printf("\n Enter no of terms");
scanf("%d",&n);
pthread_create(&pThread, 0, producer, 0);
pthread_join(pThread,NULL);
pthread_create(&cThread, 0, consumer,0);
pthread_join(cThread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
In the producer function with the help of thread (pThread) I took user input for an array and then I tried to print the same array in the consumer function with the help of cThread. But I am only able to print just the first element of the array. What are the changes that I need to make so that I can get the whole array as output?
Answer to your question is you are doing pthread_join() for a thread before creating new thread. It means, you are exiting the first thread before starting new thread. So all operations are completely serial.
You should start both the threads and then join them as below.
pthread_create(&pThread, 0, producer, 0); //Check return value and handle error
pthread_create(&cThread, 0, consumer,0); //Check return value and handle error
pthread_join(pThread,NULL);
pthread_join(cThread, NULL);
But there is one more issue in you code. In consumer() function you are unlocking mutex inside for loop. I assume, you want user to read one input from user in producer thread and print that input value in consumer thread. In that case you have to move mutex unlock call outside the loop. Please note that, pthread_cond_wait() will internally unlock the mutex during wait. Also, you need to print value after pthread_cond_wait() to ensure that user has entered an input value.
pthread_mutex_lock(&mutex);
for (i = 0; i < n; i++)
{
pthread_cond_wait(&cond, &mutex);
printf("%d\n",buffer[i]);
}
pthread_mutex_unlock(&mutex);
I have written this answer just for the interesting part below!
Calling pthread_create(); for a thread does not mean that the thread starts running immediately. Multiple threads can run in a random (should be assumed) order.
So.. If consumer thread starts very late i.e. if it starts after two or more pthread_cond_signal(&cond); from producer thread, then consumer thread will enter deadlock situation as it does exact n number of pthread_cond_wait() calls. Please note that, a pthread_cond_signal() is missed if no thread is doing pthread_cond_wait() at that exact time instant.
So.. you need to ensure that consumer thread has started before start reading input from producer thread.
There are multiple ways to achieve this. One way is to poll using a global flag and mutex. (You can use another mutex-condition-signal combination for this.)
void *producer(void *arg) {
while(1) {
pthread_mutex_lock(&mutex);
if(1 == consumerStartedFlag) {
pthread_mutex_unlock(&mutex);
break;
}
pthread_mutex_unlock(&mutex);
usleep(1000); //Sleep for 1ms to prevent this thread from consuming large CPU
}
//Rest of the producer functionality
}
void *consumer(void *arg) { // For printing the input array
pthread_mutex_lock(&mutex);
consumerStartedFlag = 1; //Global flag, intialize it to zero in main before starting threads.
pthread_mutex_unlock(&mutex);
//Rest of the consumer functionality
}
Now, if producer starts first, it will wait in while loop. If consumer starts first, it will wait in pthread_cond_wait().
Update 1 Based on a comment below.
In producer(), scanf() is inside mutex lock. It will block the consumer indefinately. So you may not get the output properly. So Put scanf() outside lock as below.
scanf(" %d",&buffer[i]); //Remove '\n' from scanf() as it will block your scanf call.
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
I have a threadpool of workers. Each worker executes this routine:
void* worker(void* args){
...
pthread_mutex_lock(&mtx);
while (queue == NULL && stop == 0){
pthread_cond_wait(&cond, &mtx);
}
el = pop(queue);
pthread_mutex_unlock(&mtx);
...
}
main thread:
int main(){
...
while (stop == 0){
...
pthread_mutex_lock(&mtx);
insert(queue, el);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
...
}
...
}
Then I have a signal handler that executes this code when it receives a signal:
void exit_handler(){
stop = 1;
pthread_mutex_lock(&mtx);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mtx);
}
I have omitted declarations and initialization, but the original code has them.
After a signal is received most of the time it's all ok, but sometimes it seems that some worker threads stay in the wait loop because they don't see that the variable stop is changed and/or they are not waken up by the broadcast.
So the threads never end.
What I am missing?
EDIT: stop=1 moved inside the critical section in exit_handler. The issue remains.
EDIT2: I was executing the program on a VM with Ubuntu. Since the code appears to be totally right I tried to change VM and OS (XUbuntu) and now it seems to work correctly. Still don't know why, anyone has an idea?
Some guessing here, but it's too long for a comment, so if this is wrong, I will delete. I think you may have a misconception about how pthread_cond_broadcast works (at least something I've been burned with in the past). From the man page:
The pthread_cond_broadcast() function shall unblock all threads
currently blocked on the specified condition variable cond.
Ok, that make sense, _broadcast awakens all threads currently blocked on cond. However, only one of the awakened threads will then be able to lock the mutex after they're all awoken. Also from the man page:
The thread(s) that are unblocked shall contend for the mutex according
to the scheduling policy (if applicable), and as if each had called
pthread_mutex_lock().
So this means that if 3 threads are blocked on cond and _broadcast is called, all 3 threads will wake up, but only 1 can grab the mutex. The other 2 will still be stuck in pthread_cond_wait, waiting on a signal. Because of this, they don't see stop set to 1, and exit_handler (I'm assuming a Ctrl+c software signal?) is done signaling, so the remaining threads that lost the _broadcast competition are stuck in limbo, waiting on a signal that will never come, and unable to read that the stop flag has been set.
I think there are 2 options to work-around/fix this:
Use pthread_cond_timedwait. Even without being signaled, this will return from waiting at the specified time interval, see that stop == 1, and then exit.
Add pthread_cond_signal or pthread_cond_broadcast at the end of your worker function. This way, right before a thread exits, it will signal the cond variable allowing any other waiting threads to grab the mutex and finish processing. There is no harm in signaling a conditional variable if no threads are waiting on it, so this should be fine even for the last thread.
EDIT: Here is an MCVE that proves (as far as I can tell) that my answer above is wrong, heh. As soon as I press Ctrl+c, the program exits "immediately", which says to me all the threads are quickly acquiring the mutex after the broadcast, seeing that stop is false, and exiting. Then main joins on the threads and it's process over.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#define NUM_THREADS 3
#define STACK_SIZE 10
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
volatile bool stop = false;
int stack[STACK_SIZE] = { 0 };
int sp = 0; // stack pointer,, also doubles as the current stack size
void SigHandler(int sig)
{
if (sig == SIGINT)
{
stop = true;
}
else
{
printf("Received unexcepted signal %d\n", sig);
}
}
void* worker(void* param)
{
long tid = (long)(param);
while (stop == false)
{
// acquire the lock
pthread_mutex_lock(&m);
while (sp <= 0) // sp should never be < 0
{
// there is no data in the stack to consume, wait to get signaled
// this unlocks the mutex when it is called, and locks the
// mutex before it returns
pthread_cond_wait(&c, &m);
}
// when we get here we should be guaranteed sp >= 1
printf("thread %ld consuming stack[%d] = %d\n", tid, sp-1, stack[sp-1]);
sp--;
pthread_mutex_unlock(&m);
int sleepVal = rand() % 10;
printf("thread %ld sleeping for %d seconds...\n", tid, sleepVal);
sleep(sleepVal);
}
pthread_exit(NULL);
}
int main(void)
{
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
srand(time(NULL));
for (long i=0; i<NUM_THREADS; i++)
{
int rc = pthread_create(&threads[i], &attr, worker, (void*)i);
if (rc != 0)
{
fprintf(stderr, "Failed to create thread %ld\n", i);
}
}
while (stop == false)
{
// produce data in bursts
int numValsToInsert = rand() % (STACK_SIZE - sp);
printf("main producing %d values\n", numValsToInsert);
// acquire the lock
pthread_mutex_lock(&m);
for (int i=0; i<numValsToInsert; i++)
{
// produce values for the stack
int val = rand() % 10000;
// I think this should already be guaranteed..?
if (sp+1 < STACK_SIZE)
{
printf("main pushing stack[%d] = %d\n", sp, val);
stack[sp++] = val;
// signal the workers that data is ready
//printf("main signaling threads...\n");
//pthread_cond_signal(&c);
}
else
{
printf("stack full!\n");
}
}
pthread_mutex_unlock(&m);
// signal the workers that data is ready
printf("main signaling threads...\n");
pthread_cond_broadcast(&c);
int sleepVal = 1;//rand() % 5;
printf("main sleeping for %d seconds...\n", sleepVal);
sleep(sleepVal);
}
for (long i=0; i<NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
return 0;
}