Guest User

Untitled

a guest
Mar 15th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<pthread.h>
  3.  
  4. pthread_cond_t cond;
  5. pthread_mutex_t mutex;
  6.  
  7. void *thread_func1(void* arg){
  8.   printf("thread1 started\n");
  9.   printf("thread1: signalling\n");
  10.   pthread_cond_signal(&cond);
  11.   printf("thread1: signalled\n");
  12.   printf("thread1: exiting\n");
  13.   pthread_exit(0);
  14. }
  15.  
  16. void *thread_func2(void* arg){
  17.   printf("thread2 started\n");
  18.   printf("thread2: waiting for signal..\n");
  19.   pthread_cond_wait(&cond, &mutex);
  20.   printf("thread2: signal received\n");
  21.   printf("thread2: exiting\n");
  22.   pthread_exit(0);
  23. }
  24.  
  25. int main(int argc, char** argv){
  26.   pthread_t thread1, thread2;
  27.  
  28.   pthread_create(&thread1, NULL, thread_func1, NULL);
  29.   pthread_create(&thread2, NULL, thread_func2, NULL);
  30.  
  31.   pthread_join(thread1, 0);
  32.   pthread_join(thread2, 0);
  33.   return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment