【Unix环境高级编程】条件变量

摘要:
pthread_ cond_ wait(pthread_cond_t*秒,pthread_mutex_t*互斥);分析:函数函数函数:块等待一个条件变量块等待条件变量cond(参数1)释放主互斥锁(解锁互斥锁),相当于pthread_互斥锁解锁(&mutex);{步骤1和2是一个原子操作}当被唤醒时,pthread_cond_wait函数返回、解除锁定并重新获取互斥锁
pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

分析:

  •  函数作用:阻塞等待一个条件变量
  • 阻塞等待条件变量cond(参数1)满足
  • 释放已掌握的互斥锁(解锁互斥量),相当于pthread_mutex_unlock(&mutex); {1、2两步为一个原子操作}
  • 当被唤醒,pthread_cond_wait函数返回,解除阻塞并重新获取互斥锁pthread_mutex_lock(&mutex);

 1. 示例

 1 #include <pthread.h> 
 2 #include <unistd.h>
 3 #include <stdlib.h>
 4 #include <stdio.h>
 5  
 6 struct msg
 7 {
 8     struct msg *next;
 9     int num;
10 };
11  
12 struct msg *head;
13 struct msg *mp;
14  
15 pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;  //静态初始化:一个条件变量和一个互斥量 
16 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
17  
18 void *consumer(void *p)
19 {
20     for (; ;)
21     {
22         pthread_mutex_lock(&lock);
23         while (head == NULL)                           //头指针为空,说明没有节点 可以为if吗 
24             pthread_cond_wait(&has_product, &lock);
25         mp = head;
26         head = mp->next;                               //模拟消费掉一个产品
27         pthread_mutex_unlock(&lock);
28         printf("-consume-----%d
", mp->num);
29         free(mp);
30         sleep(rand() % 5);
31     }
32 }
33  
34 void *producer(void *p)
35 {
36     for (; ;) 
37     {
38         mp = malloc(sizeof(struct msg));
39         mp->num = rand() % 1000 + 1;                  //模拟生产一个产品
40         printf("-Produce----%d
", mp->num);
41  
42         pthread_mutex_lock(&lock);
43         mp->next = head;
44         head = mp;
45         pthread_mutex_unlock(&lock);
46  
47         pthread_cond_signal(&has_product);          //将等待在该条件变量上的一个线程唤醒
48         sleep(rand() % 5);
49     }
50 }
51  
52 int main()
53 {
54     pthread_t pid, cid;
55     srand(time(NULL));
56     pthread_create(&pid, NULL, producer, NULL);
57     pthread_create(&cid, NULL, consumer, NULL);
58     pthread_join(pid, NULL);
59     pthread_join(cid, NULL);
60     return 0;
61 }

免责声明:文章转载自《【Unix环境高级编程】条件变量》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Lattice Diamond与modelsim联合仿真环境设置爬虫实战(三) 用Python爬取拉勾网下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

警告:Nvidia 官方二进制驱动严重问题

昨日 Nvidia 官方闭源驱动爆出重大缺陷,可能导致显卡风扇停转继而导致 GPU 核心温度过高。 该问题出现在 Win32 196.75 版和 Unix 系列 195.36.08 和 195.36.03 驱动上。对于 Unix 类系统的驱动, Nvidia 建议用户降级至 190.53 或 195.30 驱动。 对于部分新近的发行版,可以先使用开源的 N...

GDB调试器

/*this project used for gdb debug c programs*//*At first,using compile command turn out the executable file. exp: gcc -g sourcefile.c -o test.exe */        //!!!/*windows: start g...

linux网络编程--UNIX域套接字

UNIX域套接字   socket同样可以用于本地通信   创建套接字时使用本地协议PF_UNIX(或PF_LOCAL)PF_LOCAL   分为流式套接字和用户数据报套接字   和其他进程间通信方式相比使用方便。效率更高   用于前后台进程通信 本地地址结构:  struct sockaddr_un {   sa_family_t sun_family;...

python 替换windows换行符为unix格式

windows 默认换行符为 ; unix默认换行符为 ; 所以当win下编辑的脚本在linux下显示末尾多了^M:  换行符修改为同一的unix格式脚本如下: 1 def run(path,file): 2 for file in files: 3 file = path+'\'+file 4 f = o...

【RHEL/CentOS】类Unix上5个最佳开源备份工具Bacula/Amanda/Backupninja/Backuppc/UrBackup

当为一个企业选择备份工具的时,都考虑什么呢?确定正在部署的软件具有下面的特性:开源软件–务必要选择那些源码可以免费获得,并且可以修改的软件。确信可以恢复你的数据,即使是软件供应商/项目停止继续维护这个软件,或拒绝继续为这个软件提供补丁;跨平台支持–确定备份软件可以很好的运行各种需要部署的桌面操作系统和服务器系统;数据格式–一种开放的数据格式可以让你能够恢...

zipinfo

总览 SYNOPSIS zipinfo [-12smlvhMtTz] file[.zip] [file(s)...] [-xxfile(s)...] unzip-Z [-12smlvhMtTz] file[.zip] [file(s)...] [-xxfile(s)...] 描述 DESCRIPTION zipinfo 列出某个ZIP档案中的所包含文件的技...