码迷,mamicode.com
首页 > 其他好文 > 详细

thread_22

时间:2014-09-27 01:40:18      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   on   c   

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
pthread_cond_t  cond;
pthread_mutex_t mp;
 void *consumer1(void *p)
 {
         pthread_mutex_lock(&mp);
                printf("wait>>>(1)\n");
                 pthread_cond_wait(&cond, &mp);
          pthread_mutex_unlock(&mp);
         printf("wake <<<(1)\n");
                return NULL;
 }
 void *consumer2(void *p)
 {
        pthread_mutex_lock(&mp);
                printf("wait>>>(2)\n");
                pthread_cond_wait(&cond, &mp);
        pthread_mutex_unlock(&mp);
        printf("wake <<<(2)\n");
                return NULL;
}
void *consumer3(void *p)
 {       
         pthread_mutex_lock(&mp);
                 printf("wait>>>(3)\n");
                 pthread_cond_wait(&cond, &mp);
        pthread_mutex_unlock(&mp);
         printf("wake <<<(3)\n");
                 return NULL;
 }
 int main(int argc, char *argv[])
 {       
        pthread_t  t1, t2, t3;
         int ret;
        struct sched_param sched;
         sched.__sched_priority = 10;
          
         pthread_cond_init(&cond, NULL);
         pthread_mutex_init(&mp, NULL);
         
        ret = pthread_create(&t1, NULL, consumer1, NULL);
        ret = pthread_create(&t2, NULL, consumer2, NULL);
        ret = pthread_create(&t3, NULL, consumer3, NULL);
         sleep(1);
         pthread_setschedparam(t2, SCHED_FIFO, &sched);
         sleep(1);
         pthread_cond_broadcast(&cond);
         sleep(6);
         printf("main returned\n");
      return 0;
}

 

thread_22

标签:style   blog   color   io   ar   sp   div   on   c   

原文地址:http://www.cnblogs.com/leijiangtao/p/3995847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!