Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t cond =PTHREAD_COND_INITIALIZER;
- int condition = 0;
- void send_msg()
- {
- condition = 1;
- pthread_cond_broadcast(&cond);
- }
- void resive_msg()
- {
- pthread_mutex_lock(&mutex);
- while(!condition)
- {
- pthread_cond_wait(&cond,&mutex);
- }
- pthread_mutex_unlock(&mutex);
- }
- void *createThread(void *p)
- {
- int threadID = *(int*)p;
- printf("Thread %i is waiting for message ...\n",threadID);
- resive_msg();
- printf("Thread %i reseive the message ",threadID);
- }
- int main()
- {
- int thread_cnt = 10;
- pthread_t threads[thread_cnt];
- for(int i = 0 ; i < thread_cnt; ++i)
- {
- printf("thread %i is starting\n",i);
- pthread_create(&threads[i],NULL,createThread,&i);
- }
- printf("Done\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement