pthread_cond_wait 详解
配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread_cond_signal()或pthread_cond_broadcast,把该线程唤醒,使pthread_cond_wait()通过(返回)时,该线程又自动获得该mutex。
wait,而且规范要求pthread_cond_signal至少唤醒一个pthread_cond_wait上的线程,其实有些实现为了简单在单处理器上也会唤醒多个线程.
int x,y;pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&mut);while (x <= y) {pthread_cond_wait(&cond, &mut);}/* operate on x and y */pthread_mutex_unlock(&mut);
pthread_mutex_lock(&mut);/* modify x and y */if (x > y) pthread_cond_broadcast(&cond);pthread_mutex_unlock(&mut);
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#define BUFFER_SIZE 8struct Products{int buffer[BUFFER_SIZE];/*保证存取操作的原子性 互斥性*/pthread_mutex_t locker;/*是否可读*/pthread_cond_t notEmpty;/*是否可写*/pthread_cond_t notFull;int posReadFrom;int posWriteTo;};int BufferIsFull(struct Products* products){if ((products->posWriteTo + 1) % BUFFER_SIZE == products->posReadFrom){return (1);}return (0);}int BufferIsEmpty(struct Products* products){if (products->posWriteTo == products->posReadFrom){return (1);}return (0);}/*制造产品*/。void Produce(struct Products* products, int item){/*原子操作*/pthread_mutex_lock(&products->locker);/*无空间可写入*/while (BufferIsFull(products)){pthread_cond_wait(&products->notFull, &products->locker);}/*写入数据*/products->buffer[products->posWriteTo] = item;products->posWriteTo++;if (products->posWriteTo >= BUFFER_SIZE)products->posWriteTo = 0;/*发信*/pthread_cond_signal(&products->notEmpty);/*解锁*/pthread_mutex_unlock(&products->locker);}int Consume(struct Products* products){int item;pthread_mutex_lock(&products->locker);/*为空时持续等待,无数据可读*/while (BufferIsEmpty(products)){pthread_cond_wait(&products->notEmpty, &products->locker);}/*提取数据*/item = products->buffer[products->posReadFrom];products->posReadFrom++;/*如果到末尾,从头读取*/if (products->posReadFrom >= BUFFER_SIZE)products->posReadFrom = 0;pthread_cond_signal(&products->notFull);pthread_mutex_unlock(&products->locker);return item;}#define END_FLAG (-1)struct Products products;void* ProducerThread(void* data){int i;for (i = 0; i < 16; ++i){printf("producer: %d\n", i);Produce(&products, i);}Produce(&products, END_FLAG);return NULL;}void* ConsumerThread(void* data){int item;while (1){item = Consume(&products);if (END_FLAG == item)break;printf("consumer: %d\n", item);}return (NULL);}int main(int argc, char* argv[]){pthread_t producer;pthread_t consumer;int result;pthread_create(&producer, NULL, &ProducerThread, NULL);pthread_create(&consumer, NULL, &ConsumerThread, NULL);pthread_join(producer, (void *)&result);pthread_join(consumer, (void *)&result);exit(EXIT_SUCCESS);}
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
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(5);
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;
}
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- 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.
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- 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(&mymutex1);
- printf("wait in mythread2.\n");
- pthread_cond_wait(&mycond,&mymutex1);
- pthread_mutex_unlock(&mymutex1);
- 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;
- }
$ gcc threadTest.c -o test.exe
123@xyy ~/gcc-test
$ ./test.exe
begin main thread.
begin mythread1.
wait in mythread1.
begin mythread2.
wait in mythread2.
try to wake up mythread1 and mythread2 in main thread.
end mythread1.
end mythread1.
end main thread.
结束!
pthread_cond_wait 详解的更多相关文章
- pthread_cond_signal与pthread_cond_wait详解
转:http://blog.chinaunix.net/uid-11572501-id-3456343.html //pthread_cond_signal 只发信号,内部不会解锁,在Linux 线程 ...
- 条件变量pthread_cond_wait()和pthread_cond_signal()详解
条件变量 条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立" ...
- 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解
Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解 多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...
- 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
使用条件变量提高效率 本文是 POSIX 线程三部曲系列的最后一部分,Daniel 将详细讨论如何使用条件变量.条件变量是 POSIX 线程结构,可以让您在遇到某些条件时“唤醒”线程.可以将它们看作是 ...
- 通用线程:POSIX 线程详解,第 3 部分
通用线程:POSIX 线程详解,第 3 部分 使用条件变量提高效率 Daniel Robbins, 总裁兼 CEO, Gentoo Technologies, Inc. 简介: 本文是 POSIX 线 ...
- POSIX 线程详解(经典必看)
http://www.cnblogs.com/sunminmin/p/4479952.html 总共三部分: 第一部分:POSIX 线程详解 ...
- JUC中的AQS底层详细超详解
摘要:当你使用java实现一个线程同步的对象时,一定会包含一个问题:你该如何保证多个线程访问该对象时,正确地进行阻塞等待,正确地被唤醒? 本文分享自华为云社区<JUC中的AQS底层详细超详解,剖 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
随机推荐
- 互联网高并发之Hystrix实现服务隔离和降级
当大多数人在使用Tomcat时,多个HTTP服务会共享一个线程池,假设其中一个HTTP服务访问的数据库响应非常慢,这将造成服务响应时间延迟增加,大多数线程阻塞等待数据响应返回,导致整个Tomcat线程 ...
- 元素 "context:component-scan" 的前缀 "context" 未绑定的解决方案
在动态web项目(Dynamic Web Project)中,使用SpringMVC框架,新建Spring的配置文件springmvc.xml,添加扫描控制器 <context:componen ...
- Android框架之路——GreenDao3.2.2的使用
一.简介 GreenDAO是一个开源的安卓ORM框架,能够使SQLite数据库的开发再次变得有趣.它减轻开发人员处理低级数据库需求,同时节省开发时间. SQLite是一个令人敬畏的内嵌的关系数据库,编 ...
- 【Swoole】简单安装与创建TCP服务器
pecl install swoole PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了php语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据 ...
- EF切换到Mysql数据库,更改web.config
1)引用: MySql.Data.dll,MySql.Data.Entity.dll,MySql.Data.Entity.EF6.dll 2)添加: <system.data> <D ...
- TODO 正则表达式
TODO: break it. \btodo\b.* FIXME: break it. \bfixme\b.* TODO: [Bob] break it. \btodo\s*:\s*\[bob\].*
- hdu 1517 A Multiplication Game(必胜态,必败态)
A Multiplication Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 51nod 1732 LCS变形
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1732 1732 51nod婚姻介绍所 题目来源: 原创 基准时间限制:1 ...
- python 百题计划
一.基础篇 想要像类似执行shell脚本一样执行Python脚本,需要在py文件开头加上什么?KEY:#!/usr/bin/env python Python解释器在加载 .py 文件中的代码时,会对 ...
- linux命令学习笔记(30): chown命令
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID: 文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷 ...