0

This a pthread code, which cannot jump out of the while loop even though I have set up a condition variable to wait for the signal.

Any help is appreciated.

  #include <pthread.h>
  #include <iostream>
  using namespace std; 

  pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;

  int counter = 0; int WATCHCOUNT = 12 ;int totalCount = 10 ;
  int threadsID[3] = {0,1,2};
  pthread_cond_t cout_theashold_cv = PTHREAD_COND_INITIALIZER ; 

  void* watchCount(void *a) ;

  void* increaseCount(void *a)
 {
for (int i =0; i < totalCount ; ++i)
{
    pthread_mutex_lock(&myMutex);
    ++counter ;         

    cout << "I am thread " << *((int *)a) << " I increase count to " << counter << endl ;
    if (counter == WATCHCOUNT)
    {
        cout << "I am thread " << *((int *)a) << " I am before send signal with counter as " << counter << endl ;
        pthread_cond_signal(&cout_theashold_cv);
    }
    pthread_mutex_unlock(&myMutex);
  }
 } 

   int main()
   {
  pthread_t threads[3];
  pthread_create(&threads[0], NULL, increaseCount, (void *) &(threadsID[0]));
  pthread_create(&threads[1], NULL, increaseCount, (void *) &(threadsID[1]));   
  pthread_create(&threads[2], NULL, watchCount, (void *) &(threadsID[2]));
  for (int i = 0 ; i < 3 ; ++i)
  {
      pthread_join(threads[i], NULL);
  }
   return 0 ; 

 }
  // wait for cond var 
  void* watchCount(void *a)
  {
        pthread_mutex_lock(&myMutex);
        cout << "I am thread " << *((int *)a) << " I am watching count " << counter << endl ;

   //while (counter <= WATCHCOUNT)
   while (counter <= INT_MAX)
   {
        cout << "I am thread " << *((int *)a) << " before watch count current count is " << counter << endl ;
    pthread_cond_wait(&cout_theashold_cv, &myMutex);

    break ;

    cout << "I am thread " << *((int *)a) << " after watch count current count is " << counter << endl ;

}

cout << "I am thread " << *((int *)a) << " after loop and watch count current count is " << counter << endl ;

pthread_mutex_unlock(&myMutex);
}
Deanie
  • 2,316
  • 2
  • 19
  • 35
user1002288
  • 4,860
  • 10
  • 50
  • 78

2 Answers2

1

Both thread 1 and 2 may have finished before thread 3 even starts waiting on the conditional variable.

This means it will never receive the signal.

Start thread 3 first. Make sure it has reached and is waiting on the conditional before even attempting to start 1 and 2.

What you really wanted here was a counted semaphore (like a conditional variable but it keeps a count of the number of signal() and wait() calls). If it has had more signals than waits then the thread is not stopped.

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
1

Your program is a victim of race conditions. There are several problems but the main ones seems to be:

Many times by the time your third thread which calls "watchCount" goes into the condition wait(), the first 2 thread have already passed the value of counter where the signal is called. So a signal gets called before even the thread has started to wait. Hence your third thread keeps waiting forever. If you change the condition to signal counter>=WATCHCOUNT you may have better luck

Sid
  • 7,511
  • 2
  • 28
  • 41