pthread 您所在的位置:网站首页 c语言线程等待和唤醒一样吗知乎 pthread

pthread

#pthread| 来源: 网络整理| 查看: 265

实例2 pthread_cond_broadcast的使用 [cpp]  view plaincopy #include    #include    #include    #include    #include    pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER;   pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER;   pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;   void *mythread1(void *param)   {       printf("begin mythread1.\n");       pthread_mutex_lock(&mymutex1);       printf("wait in mythread1.\n");       pthread_cond_wait(&mycond,&mymutex1);       pthread_mutex_unlock(&mymutex1);       printf("end mythread1.\n");       return NULL;   }   void *mythread2(void *param)   {       printf("begin mythread2.\n");       pthread_mutex_lock(&mymutex2);       printf("wait in mythread2.\n");       pthread_cond_wait(&mycond,&mymutex2);       pthread_mutex_unlock(&mymutex2);       printf("end mythread2.\n");       return NULL;   }   int main(void)   {       printf("begin main thread.\n");       int i;       pthread_t tid1,tid2;       pthread_create(&tid1,NULL,mythread1,NULL);       pthread_create(&tid2,NULL,mythread2,NULL);       sleep(2);       printf("try to wake up mythread1 and mythread2 in main thread.\n");       if(pthread_cond_broadcast(&mycond)){           printf("error\n");           return 1;       }       void *res;       pthread_join(tid1, &res);       pthread_join(tid2, &res);       printf("end main thread.\n");       return 0;   }   运行结果:

begin main thread. begin mythread1. wait in mythread1. begin mythread2. wait in mythread2. end mythread2. try to wake up mythread1 and mythread2 in main thread. end mythread1. end main thread.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有