条件变量

条件变量:

       条件变量本身不是锁!但它也可以造成线程阻塞。通常与互斥锁配合使用。给多线程提供一个会合的场所。

主要应用函数:

       pthread_cond_init函数

       pthread_cond_destroy函数

       pthread_cond_wait函数

       pthread_cond_timedwait函数

       pthread_cond_signal函数

       pthread_cond_broadcast函数

以上6 个函数的返回值都是:成功返回0, 失败直接返回错误号。

       pthread_cond_t类型   用于定义条件变量

       pthread_cond_t cond;

pthread_cond_init函数

初始化一个条件变量

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);      

参2:attr表条件变量属性,通常为默认值,传NULL即可

也可以使用静态初始化的方法,初始化条件变量:

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

pthread_cond_destroy函数

销毁一个条件变量

int pthread_cond_destroy(pthread_cond_t *cond);

pthread_cond_wait函数

阻塞等待一个条件变量

    int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);

函数作用:

  1. 阻塞等待条件变量cond(参1)满足
  2. 释放已掌握的互斥锁(解锁互斥量)相当于pthread_mutex_unlock(&mutex);

 1.2.两步为一个原子操作。

  1. 当被唤醒,pthread_cond_wait函数返回时,解除阻塞并重新申请获取互斥锁pthread_mutex_lock(&mutex);

pthread_cond_timedwait函数

限时等待一个条件变量

int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);

       参3:    参看man sem_timedwait函数,查看struct timespec结构体。

              struct timespec {

                     time_t tv_sec;        /* seconds */ 秒

                     long   tv_nsec;    /* nanosecondes*/ 纳秒

              }                                                      

形参abstime:绝对时间。                                                               

如:time(NULL)返回的就是绝对时间。而alarm(1)是相对时间,相对当前时间定时1秒钟。

                     struct timespec t = {1, 0};

                     pthread_cond_timedwait (&cond, &mutex, &t); 只能定时到 1970年1月1日 00:00:01秒(早已经过去)

              正确用法:

                     time_t cur = time(NULL); 获取当前时间。

struct timespec t;  定义timespec 结构体变量t

                     t.tv_sec = cur+1; 定时1秒

pthread_cond_timedwait (&cond, &mutex, &t); 传参                          参APUE.11.6线程同步条件变量小节

              在讲解setitimer函数时我们还提到另外一种时间类型:

        struct timeval {

             time_t      tv_sec;  /* seconds */ 秒

             suseconds_t tv_usec; /* microseconds */ 微秒

        };

pthread_cond_signal函数

唤醒至少一个阻塞在条件变量上的线程

int pthread_cond_signal(pthread_cond_t *cond);

pthread_cond_broadcast函数

唤醒全部阻塞在条件变量上的线程

    int pthread_cond_broadcast(pthread_cond_t *cond);

生产者消费者条件变量模型

线程同步典型的案例即为生产者消费者模型,而借助条件变量来实现这一模型,是比较常见的一种方法。假定有两个线程,一个模拟生产者行为,一个模拟消费者行为。两个线程同时操作一个共享资源(一般称之为汇聚),生产向其中添加产品,消费者从中消费掉产品。

看如下示例,使用条件变量模拟生产者、消费者问题:

#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>struct msg {struct msg *next;int num;
};
struct msg *head;pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;void *consumer(void *p)
{struct msg *mp;for (;;) {pthread_mutex_lock(&lock);while (head == NULL) {           //头指针为空,说明没有节点    可以为if吗pthread_cond_wait(&has_product, &lock);}mp = head;      head = mp->next;                //模拟消费掉一个产品pthread_mutex_unlock(&lock);printf("-Consume ---%d\n", mp->num);free(mp);sleep(rand() % 5);}
}
void *producer(void *p)
{struct msg *mp;while (1) {mp = malloc(sizeof(struct msg));mp->num = rand() % 1000 + 1;        //模拟生产一个产品printf("-Produce ---%d\n", mp->num);pthread_mutex_lock(&lock);mp->next = head;head = mp;pthread_mutex_unlock(&lock);pthread_cond_signal(&has_product);  //将等待在该条件变量上的一个线程唤醒sleep(rand() % 5);}
}
int main(int argc, char *argv[])
{pthread_t pid, cid;srand(time(NULL));pthread_create(&pid, NULL, producer, NULL);pthread_create(&cid, NULL, consumer, NULL);pthread_join(pid, NULL);pthread_join(cid, NULL);return 0;
}

运行结果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190820$ ./conditionVar_product_consumer

-Produce ---276

-Consume ---276

-Produce ---514

-Consume ---514

-Produce ---889

-Consume ---889

^C

条件变量的优点:

       相较于mutex而言,条件变量可以减少竞争。

如直接使用mutex,除了生产者、消费者之间要竞争互斥量以外,消费者之间也需要竞争互斥量,但如果汇聚(链表)中没有数据,消费者之间竞争互斥锁是无意义的。有了条件变量机制以后,只有生产者完成生产,才会引起消费者之间的竞争。提高了程序效率。

/***
condition.c
***/
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>struct msg
{struct msg *next;int num;
};struct msg *head;
struct msg *mp;pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;void *consumer(void *p)
{for(;;){pthread_mutex_lock(&lock);while(head == NULL){pthread_cond_wait(&has_product,&lock);}mp = head;head = mp->next;pthread_mutex_unlock(&lock);printf("-Consumer ---%d\n",mp->num);free(mp);mp = NULL;sleep(rand() % 5);}
}void *producer(void *p)
{for(;;){mp = malloc(sizeof(struct msg));mp->num = rand() %1000 + 1;printf("-Producer ---%d\n",mp->num);pthread_cond_signal(&has_product);sleep(rand() % 5);}
}int main()
{pthread_t pid,cid;srand(time(NULL));pthread_create(&pid,NULL,producer,NULL);pthread_create(&cid,NULL,consumer,NULL);pthread_join(pid,NULL);pthread_join(cid,NULL);return 0;
}

运行结果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190820$ ./condition

-Producer ---431

-Producer ---160

-Producer ---719

-Producer ---240

-Producer ---304

-Producer ---408

-Producer ---946

-Producer ---619

-Producer ---412

-Producer ---997

-Producer ---489

-Producer ---295

^C

 

转载于:https://www.cnblogs.com/wanghao-boke/p/11389840.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384859.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

文件锁

借助 fcntl函数来实现锁机制。 操作文件的进程没有获得锁时&#xff0c;可以打开&#xff0c;但无法执行read、write操作。 fcntl函数&#xff1a; 获取、设置文件访问控制属性。 int fcntl(int fd, int cmd, ... /* arg */ ); 参2&#xff1a; F_SETLK (struct flock *) 设置…

进程间同步

互斥量mutex 进程间也可以使用互斥锁&#xff0c;来达到同步的目的。但应在pthread_mutex_init初始化之前&#xff0c;修改其属性为进程间共享。mutex的属性修改函数主要有以下几个。 主要应用函数&#xff1a; pthread_mutexattr_t mattr 类型&#xff1a; 用于定义…

Python3字符串

字符串是Python中最常用的数据类型&#xff0c;可以使用单引号或双引号来创建字符串 创建字符串很简单&#xff0c;为变量分配一个值即可。 val1 ‘hello world’ var2 “Runoob” Python访问字符串的值 Python不支持单字符类型&#xff0c;单字符在Python中也是作为 一个字符…

服务器客户端编程

server 下面通过最简单的客户端/服务器程序的实例来学习socket API。 server.c的作用是从客户端读字符&#xff0c;然后将每个字符转换为大写并回送给客户端。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #incl…

Python3元组

Python的元组与列表相似&#xff0c;不同之处在于元组的元素不能修改 元组使用小括号&#xff0c;列表使用方括号 元组创建很简单&#xff0c;只需要在括号中添加元素&#xff0c;并使用逗号隔开即可。 创建空元组 tup1 (); tup2 (1,) 元组只包含一个元素时&#xff0c;需要在…

Python3字典

字典是另一种可变容器模型&#xff0c;可存储任意类型的对象。 字典的每个键值(key>value)对用冒号分隔&#xff0c;每个对之间用逗号分隔&#xff0c;整个字典包括在花括号里&#xff0c;格式如下 d {key1 : value,key2 : value2} 键必须是唯一&#xff0c;但值则不必。 值…

线程回收

pthread_join函数 阻塞等待线程退出&#xff0c;获取线程退出状态 其作用&#xff0c;对应进程中 waitpid() 函数。 int pthread_join(pthread_t thread, void **retval); 成功&#xff1a;0&#xff1b;失败&#xff1a;错误号 参数&#xff1a;thread&#xff1a;线…

Python3数字

Python3数字数据类型用于存储数值。 数据类型是不允许改变的&#xff0c;这就意味着&#xff0c;如果改变数字数据类型的值&#xff0c;将重新分配内存空间。 Python支持三种不同不同的数值类型&#xff1a; 整型&#xff08;int&#xff09;&#xff1a;通常是被称为整型或整数…

多进程服务器

注意&#xff1a;包含了“wrap.c” 和“wrap.h”文件在上篇博客中 /*** server.c ***/ #include<stdio.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<signal.h> #include<sys/wait.h> #include<ctype…

服务器之select

select select能监听的文件描述符个数受限于FD_SETSIZE,一般为1024&#xff0c;单纯改变进程打开的文件描述符个数并不能改变select监听文件个数解决1024以下客户端时使用select是很合适的&#xff0c;但如果链接客户端过多&#xff0c;select采用的是轮询模型&#xff0c;会大…

服务器之poll

poll服务器方法采用将监听端口用数组存放起来&#xff0c;这样就不需要轮询的监听整个文件描述符了 #include <poll.h> int poll(struct pollfd *fds, nfds_t nfds, int timeout);struct pollfd {int fd; /* 文件描述符 */short events; /* 监控的事件 */short revents; …

Mysql数据库简单使用(二)

Mysql导入.sql文件 进入数据库&#xff08;要导入的数据库&#xff09;数据库中有要导入.sql文件名的数据库&#xff0c;没有则新建。source 路径文件名souce /home/robot/csql.sql 数据库文件.sql文件放在/home/robot目录下 按照时间删除数据库数据 DELETE FROM 表名 WHERE 时…

Python3集合

集合&#xff08;set&#xff09;是一个无序的不重复元素序列。 可以使用大括号{ } 或set&#xff08;&#xff09;函数来创建集合&#xff0c;注意&#xff1a;创建一个空集合必须用set(),{ }是用来创建一个空字典的。 创建格式&#xff1a; param {value01,value02,…} set(…

Python3条件判断

if语句&#xff1a; Python中if语句的一般形式如下&#xff1a; if condition_1:statement_block_1 elif condition_2:statement_block_2 else:statement_block_3 if语句关键词&#xff1a; if – elif – else 注意&#xff1a; 每个条件后面要使用冒号:使用缩进来划分语句块&…

Python3循环

Python中while语句的一般形式&#xff1a; while 判断条件: 语句 同样需要注意冒号和缩进&#xff0c;另外在Python中没有do…while循环 下面的实例计算1到100总和 ##calc.py n 100sum 0 counter 1 while counter < n:sum sum countercounter 1print("total from…

Python3迭代器和生成器

迭代器 迭代是Python最强大的功能之一&#xff0c;是访问元素集合的一种方法。 迭代器是一个可以记住遍历的位置的对象。 迭代器对象从集合的第一个元素开始访问&#xff0c;直到所有的元素被访问完结束&#xff0c;迭代器只能向前不会后退。 迭代器有两个基本方法&#xff0c;…

Pythton3实例

计算1-100之和 #add.py n 0 sum 0 for n in range(0,101):sum n print(sum) 实现99乘法法则 #mul.py i 1 while i < 9:j 1while j < i:mut j*iprint("%d * %d %d"%(j,i,mut),end" ")j 1print(" ")i 1 运算结果: robotubuntu:~/wa…

Python3函数

函数是组织好的&#xff0c;可重复使用的&#xff0c;用来实现单一&#xff0c;或相关功能的代码段。 函数能提高应用的模块性&#xff0c;和代码的重复使用率。 定义一个函数 可以定义一个由自己想要功能的函数&#xff0c;以下是简单规则&#xff1a; l 函数代码块是以def关…

epoll函数

epoll是Linux下多路复用IO接口select/poll的增强版本&#xff0c;它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率&#xff0c;因为它会复用文件描述符集合来传递结果而不用迫使开发者每次等待事件之前都必须重新准备要被侦听的文件描述符集合&#xff0…

epoll事件模型

事件模型 EPOLL事件有两种模型&#xff1a; Edge Triggered (ET) 边缘触发只有数据到来才触发&#xff0c;不管缓存区中是否还有数据。 Level Triggered (LT) 水平触发只要有数据都会触发。 思考如下步骤&#xff1a; 假定我们已经把一个用来从管道中读取数据的文件描述符(RFD)…