linux系统编程:线程同步-条件变量(cond)
2015-07-26 11:29
751 查看
线程同步-条件变量(cond)
这个问题的同步在于两处:第一,消费者之间需要同步:同一件产品只可由一人消费。第二,当无产品可消费时,消费者需等待生产者生产后,才可继续消费,这又是一个同步问题。详细了解:生产者消费者问题。
所有运行到此处的线程被阻塞,直到条件变量的唤醒。
在阻塞的同时释放锁。
当被唤醒后,重新去获取锁。
唤醒线程的函数有两个:pthread_cond_signal和pthread_cond_broadcast,前者唤醒一个,后者唤醒所有的。
CCPP Blog 目录
生产者与消费者问题
再引入条件变量之前,我们先看下生产者和消费者问题:生产者不断地生产产品,同时消费者不断地在消费产品。这个问题的同步在于两处:第一,消费者之间需要同步:同一件产品只可由一人消费。第二,当无产品可消费时,消费者需等待生产者生产后,才可继续消费,这又是一个同步问题。详细了解:生产者消费者问题。
条件变量
条件变量是利用线程间共享的全局变量进行同步的一种机制,并且条件变量总是和互斥锁结合在一起。相关函数
pthread_cond_t //条件变量类型 pthread_cond_init pthread_cond_destroy pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *) pthread_cond_timedwait pthread_cond_signal pthread_cond_broadcast其中,需要着重理解的是pthread_cond_wait()方法,它有三个作用:
所有运行到此处的线程被阻塞,直到条件变量的唤醒。
在阻塞的同时释放锁。
当被唤醒后,重新去获取锁。
唤醒线程的函数有两个:pthread_cond_signal和pthread_cond_broadcast,前者唤醒一个,后者唤醒所有的。
生产者与消费者示例代码
运用条件变量,我们来解决下生产者和消费者问题:#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> struct goods { int id; struct goods *next; }; pthread_mutex_t m; pthread_cond_t has_product; struct goods *head; void *producer(void *argv) { struct goods *p = NULL; while (1) { pthread_mutex_lock(&m); p = malloc(sizeof(struct goods)); p->id = rand() % 100; p->next = head; head = p; printf("produce %d\n", p->id); pthread_mutex_unlock(&m); pthread_cond_signal(&has_product); //printf("produce %d\n", p->id); sleep(rand() % 2); } return (void *)0; } void *comsumer(void *argv) { struct goods *p = NULL; while (1) { pthread_mutex_lock(&m); //思考:pthread_cond_wait()的作用? while (NULL == head) pthread_cond_wait(&has_product, &m); p = head; head = head->next; printf("comsume %d\n", p->id); pthread_mutex_unlock(&m); //printf("comsume %d\n", p->id); free(p); sleep(rand() % 2); } return (void *)0; } int main(void) { int i; //初始化条件变量和互斥量 pthread_mutex_init(&m, NULL); pthread_cond_init(&has_product, NULL); head = NULL; pthread_t pro[2], com[3]; for (i = 0; i < 2; i++) pthread_create(&pro[i], NULL, producer, NULL); for (i = 0; i < 3; i++) pthread_create(&com[i], NULL, comsumer, NULL); for (i = 0; i < 2; i++) pthread_join(pro[i], NULL); for (i = 0; i < 3; i++) pthread_join(com[i], NULL); //销毁条件变量和互斥量 pthread_mutex_destroy(&m); pthread_cond_destroy(&has_product); return 0; }在代码中,我们开启两个线程作为生产者,三个线程作为消费者。产品使用链表存储,并且每次生产和消费都在链表头部发生。
CCPP Blog 目录
相关文章推荐
- linux exec函数族
- linux通过建模工具Umbrello
- VMware Workstation 里创建CentOS
- CentOS 7 运行级别的切换
- CentOS 7 运行级别的切换
- LINUX C获取文件大小
- Windows连接linux数据库
- Linux多线程与同步
- CentOS下cp命令中拷贝所有的写法
- Linux中TTY是什么意思
- Linux、Windows时间同步
- Linux通过使用Sambaserver示例
- 设置、修改Linux时区
- Linux命令学习(28) ln命令
- Linux命令学习(28) ln命令 分类: leetCode 2015-07-26 09:45 13人阅读 评论(0) 收藏
- 【Linux命令】--(9)其他常用命令
- 查看Linux服务器的所占用的IP地址
- centos 6.6默认iptable规则
- linux初学(五)之linux系统常用命令
- linux线程初学之实现打字母功能