相互排斥锁用于上锁,条件变量则用于等待。条件变量是类型为pthread_cond_t的变量。一般使用例如以下函数:

#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr);
int pthread_cond_signal(pthread_cond_t *cptr);

每一个条件变量总是有一个相互排斥锁与之关联。调用pthread_cond_wait等待某个条件为真时,还会指定其条件变量的地址和所关联的相互排斥锁的地址。

使用条件变量的生产者-消费者程序例如以下:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> #define MAXNITEMS 1000000
#define MAXNTHREADS 100 /*globals shared by threads*/
int nitems; /*read-only by producer and consumer*/
int buff[MAXNITEMS];
struct{
pthread_mutex_t mutex;
int nput; /*next index to store*/
int nval; /*next value to store*/
}put = {
PTHREAD_MUTEX_INITIALIZER
}; struct{
pthread_mutex_t mutex;
pthread_cond_t cond;
int nready; /*number ready for consumer*/
}nready={
PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER
}; int min(int a, int b)
{
return (a < b) ? (a) : (b);
} void *produce(void *), *consume(void *); int
main(int argc, char **argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume; if(argc != 3){
printf("usage:produces6 <#items> <#threads>.\n");
return -1;
}
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS); /*create all producers and one consumer*/
pthread_setconcurrency(nthreads + 1);
for(i = 0; i < nthreads; i++){
count[i] = 0;
pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
pthread_create(&tid_consume, NULL, consume, NULL); /*wait for all producers and the consumer*/
for(i = 0; i < nthreads; i++){
pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
pthread_join(tid_consume, NULL); exit(0);
} void *
produce(void *arg)
{
for(;;){
pthread_mutex_lock(&put.mutex);
if(put.nput >= nitems){
pthread_mutex_unlock(&put.mutex);
return (NULL); /*array is full, we're done*/
}
buff[put.nput] = put.nval;
put.nput++;
put.nval++;
pthread_mutex_unlock(&put.mutex); pthread_mutex_lock(&nready.mutex);
if(nready.nready == 0){
pthread_cond_signal(&nready.cond);
}
nready.nready++;
pthread_mutex_unlock(&nready.mutex); *((int *)arg) += 1;
}
} void *
consume(void *arg)
{
int i;
for(i = 0; i < nitems; i++){
pthread_mutex_lock(&nready.mutex);
while(nready.nready == 0)
pthread_cond_wait(&nready.cond, &nready.mutex);
nready.nready--;
pthread_mutex_unlock(&nready.mutex); if(buff[i] != i)
printf("buff[%d] = %d\n", i, buff[i]);
}
return(NULL);
}

Linux环境编程之同步(二):条件变量的更多相关文章

  1. UNIX环境高级编程——线程同步之条件变量以及属性

    条件变量变量也是出自POSIX线程标准,另一种线程同步机制.主要用来等待某个条件的发生.可以用来同步同一进程中的各个线程.当然如果一个条件变量存放在多个进程共享的某个内存区中,那么还可以通过条件变量来 ...

  2. Linux环境编程之同步(四):Posix信号量

    信号量是一种用于提供不同进程间或一个给定进程的不同线程间同步手段的原语.有三种类型:Posix有名信号量,使用Posix IPC名字标识.Posix基于内存的信号量,存放在共享内存区中:System ...

  3. Linux环境编程之同步(三):读写锁

    概述 相互排斥锁把试图进入我们称之为临界区的全部其它线程都堵塞住.该临界区通常涉及对由这些线程共享一个或多个数据的訪问或更新.读写锁在获取读写锁用于读某个数据和获取读写锁用于写直接作差别. 读写锁的分 ...

  4. Linux环境编程相关的文章

    Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...

  5. Linux同步机制(二) - 条件变量,信号量,文件锁,栅栏

    1 条件变量 条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足. 1.1 相关函数 #include <pthread.h>  pthread_cond_t cond ...

  6. 四十二、Linux 线程——线程同步之条件变量之线程状态转换

    42.1 线程状态转换 42.1.1 状态转换图 42.1.2 一个线程计算,多个线程获取的案例 #include <stdio.h> #include <stdlib.h> ...

  7. (转)Linux C 多线程编程----互斥锁与条件变量

    转:http://blog.csdn.net/xing_hao/article/details/6626223 一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1. 初始化: 在 ...

  8. linux多线程同步pthread_cond_XXX条件变量的理解

    在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...

  9. Linux线程同步:条件变量

    条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足,它常和互斥锁一起使用.使用时,条件变量被用来阻塞一个线程,当条件不满足时,线程往往解开相应的互斥锁并等待条件发生变化.一旦其它 ...

随机推荐

  1. 进阶:案例六: Context Menu(静态 与 动态)

    实现: 1.add: 2.delete 3.add2 实现步骤: 1.新建属性display_text 2.创建layout 3.代码部分: add事件: METHOD onactionadd . D ...

  2. 在springmvc中配置jedis(转)

    主要学习https://github.com/thinkgem/jeesite.一下代码均参考于此并稍作修改. 1.jedis 首先,需要添加jedis: <!--jedis--> < ...

  3. 基于Hadoop技术实现的离线电商分析平台(Flume、Hadoop、Hbase、SpringMVC、highcharts)

    离线数据分析平台是一种利用hadoop集群开发工具的一种方式,主要作用是帮助公司对网站的应用有一个比较好的了解.尤其是在电商.旅游.银行.证券.游戏等领域有非常广泛,因为这些领域对数据和用户的特性把握 ...

  4. 《转》OpenStack Live Migration

    This post is based assumption that KVM as hypervisor, and Openstack is running in Grizzly on top of ...

  5. Office 2013 正式版 下载地址 带正版验证

    万众期待的正式版Office 2013 降临---英文版/中文简体版 英文版软件下载地址: office_professional_plus_2013_x86_dvd en_office_profes ...

  6. Shell 基本运算符

    Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...

  7. HttpWeb服务器之--用OO方式写

    虽然写的不是很好,但 最终解释权以及版权归13东倍所有! package com.web; import java.io.IOException; public class Test { public ...

  8. Floodlight 处理交换机增加/移除过程

         Floodlight 使用的是Netty架构,在Controller.java 入口函数中显示创建ServerBootstrap,设置套接字选项,ChannelPipeline,此时监听套接 ...

  9. Oracle 12C 简介

    2013年6月26日,Oracle Database 12c版本正式发布,首先发布的版本号是12.1.0.1.0,率先提供下载的平台有Linux和Solaris: Oracle官方下载地址: http ...

  10. 14.4.3.1 The InnoDB Buffer Pool

    14.4.3.1 The InnoDB Buffer Pool 14.4.3.2 Configuring Multiple Buffer Pool Instances 14.4.3.3 Making ...