Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 17th, 2013  |  syntax: C  |  size: 1.00 KB  |  views: 6  |  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. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10.  
  11. static int nthreads = 10;
  12. static int init_count = 0;
  13. pthread_mutex_t init_lock;
  14. pthread_cond_t init_cond;
  15. pthread_t threads[10];
  16.  
  17. void*  func(void* p)
  18. {
  19.     pthread_mutex_lock(&init_lock);
  20.     init_count++;
  21.     pthread_cond_signal(&init_cond);
  22.     pthread_mutex_unlock(&init_lock);
  23.    
  24.  
  25.     return NULL;
  26. }
  27.  
  28.  
  29.  
  30. int main(int argc, char* argv[])
  31. {
  32.  
  33.         switch(fork()) {
  34.         case 0:
  35.             break;
  36.         case -1:
  37.             perror("perror");    
  38.             exit(0);
  39.         default:             
  40.             exit(0);
  41.         }      
  42.    
  43.         pthread_mutex_init(&init_lock, NULL);
  44.         pthread_cond_init(&init_cond, NULL);
  45.         int i;     
  46.         for(i = 0; i < nthreads; ++i)
  47.         {
  48.                 pthread_create(&threads[i], NULL, func, NULL);
  49.         }
  50.  
  51.         pthread_mutex_lock(&init_lock);
  52.         while (init_count < nthreads)
  53.         {
  54.                 pthread_cond_wait(&init_cond, &init_lock);
  55.         }
  56.         pthread_mutex_unlock(&init_lock);
  57.         return 0;
  58. }