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

thread_20

时间:2014-09-27 02:10:19      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   问题   on   

#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_signal(&cond);
         sleep(6);
         printf("main returned\n");
      return 0;
}
/*当时程序在第49行出问题了,显示error: stray ‘\200’ in program
解决的办法为删除前面的空格*/

 

thread_20

标签:style   blog   color   io   ar   sp   div   问题   on   

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

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