ulvis.paste.net

Paste Search Dynamic
Recent pastes
Pthreads condition variables
  1. /******************************************************************************
  2. * FILE: condvar.c
  3. * DESCRIPTION:
  4. *   Example code for using Pthreads condition variables.  The main thread
  5. *   creates three threads.  Two of those threads increment a "count" variable,
  6. *   while the third thread watches the value of "count".  When "count"
  7. *   reaches a predefined limit, the waiting thread is signaled by one of the
  8. *   incrementing threads. The waiting thread "awakens" and then modifies
  9. *   count. The program continues until the incrementing threads reach
  10. *   TCOUNT. The main program prints the final value of count.
  11. * SOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
  12. *   et al. O'Reilly and Associates.
  13. * LAST REVISED: 07/16/09  Blaise Barney
  14. ******************************************************************************/
  15. #include <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. #define NUM_THREADS  3
  20. #define TCOUNT 10
  21. #define COUNT_LIMIT 12
  22.  
  23. int     count = 0;
  24. pthread_mutex_t count_mutex;
  25. pthread_cond_t count_threshold_cv;
  26.  
  27. void *inc_count(void *t)
  28. {
  29.   int i;
  30.   long my_id = (long)t;
  31.  
  32.   for (i=0; i < TCOUNT; i++) {
  33.     pthread_mutex_lock(&count_mutex);
  34.     count++;
  35.  
  36.     /*
  37.     Check the value of count and signal waiting thread when condition is
  38.     reached.  Note that this occurs while mutex is locked.
  39.     */
  40.     if (count == COUNT_LIMIT) {
  41.       printf("inc_count(): thread %ld, count = %d  Threshold reached. ",
  42.              my_id, count);
  43.       pthread_cond_signal(&count_threshold_cv);
  44.       printf("Just sent signal.\n");
  45.       }
  46.     printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
  47.        my_id, count);
  48.     pthread_mutex_unlock(&count_mutex);
  49.  
  50.     /* Do some work so threads can alternate on mutex lock */
  51.     sleep(1);
  52.     }
  53.   pthread_exit(null);
  54. }
  55.  
  56. void *watch_count(void *t)
  57. {
  58.   long my_id = (long)t;
  59.  
  60.   printf("Starting watch_count(): thread %ld\n", my_id);
  61.  
  62.   /*
  63.   Lock mutex and wait for signal.  Note that the pthread_cond_wait routine
  64.   will automatically and atomically unlock mutex while it waits.
  65.   Also, note that if COUNT_LIMIT is reached before this routine is run by
  66.   the waiting thread, the loop will be skipped to prevent pthread_cond_wait
  67.   from never returning.
  68.   */
  69.   pthread_mutex_lock(&count_mutex);
  70.   if (count < COUNT_LIMIT) {
  71.     printf("watch_count(): thread %ld going into wait...\n", my_id);
  72.     pthread_cond_wait(&count_threshold_cv, &count_mutex);
  73.     printf("watch_count(): thread %ld Condition signal received.\n", my_id);
  74.     count += 125;
  75.     printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
  76.     }
  77.   pthread_mutex_unlock(&count_mutex);
  78.   pthread_exit(null);
  79. }
  80.  
  81. int main(int argc, char *argv[])
  82. {
  83.   int i, rc;
  84.   long t1=1, t2=2, t3=3;
  85.   pthread_t threads[3];
  86.   pthread_attr_t attr;
  87.  
  88.   /* Initialize mutex and condition variable objects */
  89.   pthread_mutex_init(&count_mutex, null);
  90.   pthread_cond_init (&count_threshold_cv, null);
  91.  
  92.   /* For portability, explicitly create threads in a joinable state */
  93.   pthread_attr_init(&attr);
  94.   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  95.   pthread_create(&threads[0], &attr, watch_count, (void *)t1);
  96.   pthread_create(&threads[1], &attr, inc_count, (void *)t2);
  97.   pthread_create(&threads[2], &attr, inc_count, (void *)t3);
  98.  
  99.   /* Wait for all threads to complete */
  100.   for (i = 0; i < NUM_THREADS; i++) {
  101.     pthread_join(threads[i], null);
  102.   }
  103.   printf ("Main(): Waited on %d threads. Final value of count = %d. Done.\n",
  104.           NUM_THREADS, count);
  105.  
  106.   /* Clean up and exit */
  107.   pthread_attr_destroy(&attr);
  108.   pthread_mutex_destroy(&count_mutex);
  109.   pthread_cond_destroy(&count_threshold_cv);
  110.   pthread_exit (null);
  111.  
  112. }
Parsed in 0.017 seconds