This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

lacos

By: a guest on Nov 5th, 2009  |  syntax: C  |  size: 2.17 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* gcc -Wall -Wextra -ansi -pedantic -o waittest -g3 waittest.c -l pthread */
  2.  
  3. #define _XOPEN_SOURCE 500 /* SUSv2 */
  4. #include <pthread.h>      /* pthread_mutex_lock() */
  5. #include <assert.h>       /* assert() */
  6. #include <unistd.h>       /* read() */
  7. #include <stdio.h>        /* fprintf() */
  8.  
  9.  
  10. static pthread_mutex_t mutex;
  11. static pthread_cond_t condvar;
  12. static int *protected;
  13.  
  14.  
  15. static void *
  16. produce(void *ignored)
  17. {
  18.   static int msg_to_publish;
  19.   int ret;
  20.  
  21.   /* Produce message to publish. */
  22.   msg_to_publish = 1;
  23.  
  24.   /* Publish it. */
  25.   ret = pthread_mutex_lock(&mutex); assert(0 == ret);
  26.   protected = &msg_to_publish;
  27.   ret = pthread_cond_signal(&condvar); assert(0 == ret);
  28.   ret = pthread_mutex_unlock(&mutex); assert(0 == ret);
  29.  
  30.   return 0;
  31. }
  32.  
  33.  
  34. int
  35. main(void)
  36. {
  37.   int ret;
  38.  
  39.   /* Initialize mutex. */
  40.   {
  41.     pthread_mutexattr_t mattr;
  42.  
  43.     ret = pthread_mutexattr_init(&mattr); assert(0 == ret);
  44.     ret = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK);
  45.     assert(0 == ret);
  46.     ret = pthread_mutex_init(&mutex, &mattr); assert(0 == ret);
  47.     ret = pthread_mutexattr_destroy(&mattr); assert(0 == ret);
  48.   }
  49.  
  50.   /* Initialize condition variable. */
  51.   ret = pthread_cond_init(&condvar, 0); assert(0 == ret);
  52.  
  53.   /* Co-operate with producer. */
  54.   {
  55.     pthread_t producer;
  56.  
  57.     /* Start producer. */
  58.     ret = pthread_create(&producer, 0, produce, 0); assert(0 == ret);
  59.  
  60.     /* Give producer plenty of time. */
  61.     {
  62.       ssize_t rd;
  63.       char unsigned cu;
  64.  
  65.       rd = read(STDIN_FILENO, &cu, sizeof cu); assert(1 == rd);
  66.     }
  67.  
  68.     /* Consume. */
  69.     {
  70.       int *saved;
  71.  
  72.       ret = pthread_mutex_lock(&mutex); assert(0 == ret);
  73.       while (0 == protected) {
  74.         ret = pthread_cond_wait(&condvar, &mutex); assert(0 == ret);
  75.       }
  76.       saved = protected;
  77.       ret = pthread_mutex_unlock(&mutex); assert(0 == ret);
  78.  
  79.       (void)fprintf(stdout, "%d\n", *saved);
  80.     }
  81.  
  82.     /* Assimilate producer. */
  83.     ret = pthread_join(producer, 0); assert(0 == ret);
  84.   }
  85.  
  86.   /* Clean up. */
  87.   ret = pthread_cond_destroy(&condvar); assert(0 == ret);
  88.   ret = pthread_mutex_destroy(&mutex); assert(0 == ret);
  89.  
  90.   return 0;
  91. }
clone this paste RAW Paste Data