Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- int flag; /* shared variable */
- pthread_mutex_t m;
- pthread_cond_t c;
- void *threadone(void *arg)
- {
- pthread_mutex_lock(&m);
- while(flag != 1) pthread_cond_wait(&c, &m);
- pthread_mutex_unlock(&m);
- puts("one");
- return(arg);
- }
- void *threadtwo(void *arg)
- {
- puts("two");
- pthread_mutex_lock(&m);
- flag = 1;
- pthread_cond_broadcast(&c);
- pthread_mutex_unlock(&m);
- return(arg);
- }
- int main(void)
- {
- pthread_t threadid[2];
- pthread_mutex_init(&m, 0);
- pthread_cond_init(&c, 0);
- pthread_create(&threadid[0], 0, threadone, 0);
- pthread_create(&threadid[1], 0, threadtwo, 0);
- pthread_join(threadid[0], 0);
- pthread_join(threadid[1], 0);
- pthread_mutex_destroy(&m);
- pthread_cond_destroy(&c);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement