/∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗ FILE: condvar1.c

∗ DESCRIPTION:

∗ Example code for using Pthreads condition variables. The main thread

∗ creates three threads. Two of those threads increment a ”count” variable,

∗ while the third thread watches the value of ”count”. When ”count”

∗ reaches a predefined limit, the waiting thread is signaled by one of the

∗ incrementing threads. ∗

∗ SOURCE: Adapted from example code in ”Pthreads Programming”, B. Nichols

∗ et al. O’Reilly and Associates.

∗ LAST REVISED: 02/11/2002 Blaise Barney ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/

#include <pthread.h>
#include <stdio.h>

#define NUM THREADS 3 #define TCOUNT 10 #define COUNT LIMIT 12

int count = 0; int thread ids[3] = {0,1,2}; pthread mutex t count mutex; pthread cond t count threshold cv;

void ∗inc count(void ∗idp) {

int j,i;

double result=0.0;

int ∗my id = idp;

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

pthread cond signal(&count threshold cv);

      printf("inc count():  thread %d, count = %d Threshold reached.\n",

∗my id, count); }

    printf("inc count():  thread %d, count = %d, unlocking mutex\n",

∗my id, count); pthread mutex unlock(&count mutex);

/∗ Do some work so threads can alternate on mutex lock ∗/

for (j=0; j < 1000; j++)

result = result + (double)random(); }

pthread exit(NULL); }

void ∗watch count(void ∗idp) {

int ∗my id = idp;

printf("Starting watch count():  thread %d\n", ∗my id);

/∗ Lock mutex and wait for signal. Note that the pthread cond wait routine will automatically and atomically unlock mutex while it waits. Also, note that if COUNT LIMIT is reached before this routine is run by the waiting thread, the loop will be skipped to prevent pthread cond wait from never returning. ∗/ pthread mutex lock(&count mutex); while (count < COUNT LIMIT) {

pthread cond wait(&count threshold cv, &count mutex);

  printf("watch count():  thread %d Condition signal received.\n", ∗my id);

} pthread mutex unlock(&count mutex); pthread exit(NULL);

}

int main(int argc, char ∗argv[]) {

int i, rc; 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, inc count, (void ∗)&thread ids[0]); pthread create(&threads[1], &attr, inc count, (void ∗)&thread ids[1]); pthread create(&threads[2], &attr, watch count, (void ∗)&thread ids[2]);

/∗ Wait for all threads to complete ∗/ for (i = 0; i < NUM THREADS; i++) {

pthread join(threads[i], NULL); }

printf ("Main():  Waited on %d threads.  Done.\n", NUM THREADS);

/∗ Clean up and exit ∗/ pthread attr destroy(&attr); pthread mutex destroy(&count mutex); pthread cond destroy(&count threshold cv); pthread exit (NULL);

}