3线程同步:条件变量

1
条件变量

条件变量给多个线程提供了一个汇合的场所。

依赖的头文件

#include<pthread.h>

函数声明

定义分配条件变量

pthread_cond_t cond =PTHREAD_COND_INITIALIZER;

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


名称:


pthread_cond_init


功能:


initialize condition variables


头文件:


#include <pthread.h>


函数原形:


int pthread_cond_init(pthread_cond_t *restrict cond,

const pthread_condattr_t *restrict attr);


参数:


返回值:


the pthread_cond_destroy() and pthread_cond_init() functions 
shall  return zero; otherwise, an error number shall be returned to indicate the error

int pthread_cond_destroy(pthread_cond_t*cond);


名称:


pthread_cond_destroy


功能:


Destroy condition variables


头文件:


#include <pthread.h>


函数原形:


int pthread_cond_destroy(pthread_cond_t *cond);


参数:


函数说明:


在释放或废弃条件变量之前,需要毁坏它,使用此函数


返回值:


the pthread_cond_destroy() and pthread_cond_init() functions 
shall  return zero; otherwise, an error number shall be returned to indicate the error

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


名称:


pthread_cond_wait


功能:


Wait on a condition,等待某个条件是否成立。对于timewait()函数除了等待以外,可以设置一个时长。


头文件:


#include <pthread.h>


函数原形:


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


参数:


函数说明:


一旦初始化了互斥对象和条件变量,就可以等待这个条件,一个特定条件只能有一个互斥对象,而且条件变量应该表示互斥数据“内部”的一种特殊的条件更改。一个互斥条件可以用许多条件变量(例如:cond_empty,cond_full,cond_cleanup),但每个条件变量只能有一个互斥对象。


返回值:

int pthread_cond_signal(pthread_cond_t*cond);


名称:


pthread_cond_signal


功能:


signal a condition,这种情况是只有一个线程收到后执行动作。


头文件:


#include <pthread.h>


函数原形:


int pthread_cond_signal(pthread_cond_t *cond);


参数:


函数说明:


活动线程只需要唤醒第一个正在睡眠的线程。假设您只对队列添加了一个工作作业。那么只需要唤醒一个工作程序线程(再唤醒其它线程是不礼貌的!)


返回值:

int pthread_cond_broadcast(pthread_cond_t*cond);


名称:


pthread_cond_broadcast


功能:


broadcast a condition,通过广播的形式发给子线程消息,子线程竞争执行。


头文件:


#include <pthread.h>


函数原形:


int pthread_cond_broadcast(pthread_cond_t *cond);


参数:


函数说明:


如果线程更改某些共享数据,而且它想要唤醒所有正在等待的线程,则应使用 pthread_cond_broadcast
调用


返回值:

案例说明:


#include <stdlib.h>

#include <pthread.h>

#include <stdio.h>

#include <unistd.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);

/* pthread_cond_wait(&has_product, &lock);

* 1.阻塞等待has_product被唤醒,

* 2.释放互斥锁, pthread_mutex_unlock(&lock)

* 3.当被唤醒时,解除阻塞,并且重新去申请获得互斥锁 pthread_mutex_lock(&lock)

*/

while (head == NULL)

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;

for (;;) {

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_broadcast(&has_product) 唤醒等待队列上的所有线程*/

//发送信号,告诉消费者有产品了

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;

}

运行结果:

总结:从上面可以看出,消费者总是消费最先生产出来的一个。

时间: 2024-07-28 12:49:34

3线程同步:条件变量的相关文章

linux系统编程:线程同步-条件变量(cond)

线程同步-条件变量(cond) 生产者与消费者问题 再引入条件变量之前,我们先看下生产者和消费者问题:生产者不断地生产产品,同时消费者不断地在消费产品. 这个问题的同步在于两处:第一,消费者之间需要同步:同一件产品只可由一人消费.第二,当无产品可消费时,消费者需等待生产者生产后,才可继续消费,这又是一个同步问题.详细了解:生产者消费者问题. 条件变量 条件变量是利用线程间共享的全局变量进行同步的一种机制,并且条件变量总是和互斥锁结合在一起. 相关函数 pthread_cond_t //条件变量类

linux网络编程-----&gt;线程同步--&gt;条件变量

开发使用多线程过程中, 不可避免的会出现多个线程同时操作同一块共享资源, 当操作全部为读时, 不会出现未知结果, 一旦当某个线程操作中有写操作时, 就会出现数据不同步的事件. 而出现数据混乱的原因: 资源共享(独享资源则不会) 调试随机(对数据的访问会出现竞争) 线程间缺少必要的同步机制 以上三点, 前两点不能被改变. 欲提高效率, 传递数据, 资源必须共享. 只要资源共享, 就一定会出现线程间资源竞争, 只要存在竞争关系, 数据就会出现混乱. 所以只能从第三点着手, 使多个线程在访问共享资源的

pThread线程(三) 线程同步--条件变量

条件变量(Condition Variables) 参考资料:http://game-lab.org/posts/posix-thread-cn/#5.1 条件变量是什么? 条件变量为我们提供了另一种线程间同步的方法,然而,互斥量是通过控制线程访问数据来实现同步,条件变量允许线程同步是基于实际数据的值. 如果没有条件变量,程序员需要让线程不断地轮询,以检查是否满足条件.由于线程处在一个不间断的忙碌状态,所以这是相当耗资源的.条件变量就是这么一个不需要轮询就可以解决这个问题的方法. 条件变量总是跟

线程同步-条件变量

条件变量的使用:将互斥量的忙等机制改为通知机制 涉及到的函数有以下几个: int pthread_cond_destroy(pthread_cond_t *cond); /********************** *功能:条件变量的初始化 *参数:cond:条件变量 * attr:条件变量的属性 * ********************/ int pthread_cond_init(pthread_cond_t *restrict cond , const pthread_condatt

线程同步——条件变量

1.互斥量的存在问题:     互斥量是线程程序必需的工具,但它们并非万能的.例如,如果线程正在等待共享数据内某个条件出现,那会发生什么呢?它可以重复对互斥对象锁定和解锁,每次都会检查共享数据结构,以查找某个值.但这是在浪费时间和资源,而且这种繁忙查询的效率非常低. 在每次检查之间,可以让调用线程短暂地进入睡眠,比如睡眠三秒钟,但是因此线程代码就无法最快作出响应.真正需要的是这样一种方法:当线程在等待满足某些条件时使线程进入睡眠状态.一旦条件满足,就唤醒因等待满足特定条件而睡眠的线程.如果能够做

c++11线程之条件变量condition_variable(二)

题目:编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC-.依次递推. 采用C++11实现: [cpp] view plaincopyprint? #include<iostream> #include<thread> #include<mutex> #include<condition_variable> using namespace std; mut

c++11线程之条件变量condition_variable

题目:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码. [cpp] view plaincopyprint? #include<iostream> #include<thread> #include<mutex> #include<condition_variable> using namespace std; mutex m; condition_variab

多线程同步条件变量(转载)

最近看<UNIX环境高级编程>多线程同步,看到他举例说条件变量pthread_cond_t怎么用,愣是没有看懂,只好在网上找了份代码,跑了跑,才弄明白 [cpp] view plaincopy #include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/ pthread_cond_

pyhon——线程同步条件(event)

event.wait() wait未被设定时,线程会被卡住,执行不下去,一旦设定,就相当于pass event.set() 来给wait设定 event.clear() 来清除set设定 import threading import time class Boss(threading.Thread): def run(self): print("boss: 今晚加班到12.00") print(event.isSet()) event.set() time.sleep(5) prin

线程的条件变量实例

情景1: Jack开着一辆出租车来到一个网站停车.看见没人就走了.过段时间.Susan来到网站准备乘车.可是没有来,于是就等着.过了一会Mike开着车来到了这个网站,Sunsan就上了Mike的车走了.如图所看到的: 程序实现该情景: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> pthread_cond_t taxicond = PTHREAD