0
votes

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);

}
1

1 Answers

0
votes

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.