Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. pthread_mutex_t mymutex;
  5. pthread_cond_t mycond;
  6. int somevalue = 0;
  7.  
  8.  
  9.  
  10. void some_func()
  11. {
  12.     printf("fck yeaa\n");
  13. }
  14.  
  15. void *onethread()
  16. {
  17.     pthread_mutex_lock(&mymutex);
  18.  
  19.     while(somevalue == 0) pthread_cond_wait(&mycond, &mymutex);
  20.  
  21.     if(somevalue == 0xdeadbeef)
  22.         some_func();
  23.  
  24.     pthread_mutex_unlock(&mymutex);
  25. }
  26.  
  27.  
  28. void *otherthread()
  29. {
  30.     pthread_mutex_lock(&mymutex);
  31.  
  32.     somevalue = 0xdeadbeef;
  33.  
  34.     pthread_cond_broadcast(&mycond);
  35.  
  36.     pthread_mutex_unlock(&mymutex);
  37. }
  38.  
  39. int main()
  40. {
  41.     pthread_t p1;
  42.     pthread_t p2;
  43.     pthread_cond_init (&mycond, NULL);
  44.     pthread_mutex_init(&mymutex, NULL);
  45.     pthread_create(&p1, NULL, onethread, (void *) 0);
  46.     pthread_create(&p2, NULL, otherthread, (void *) 0);
  47.  
  48.     pthread_exit(NULL);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement