posix 条件变量

news/2024/5/19 0:34:21 标签: struct, signal, 任务, null
 条件变量是一中线程同步机制,需要和 pthread_mutex_t配合使用才能完成任务,典型的可以应用在“生产者、消费者”模型中。

条件变量的数据类型:pthread_cond_t

配合的函数有:

1、pthread_cond_t结构的初始化、销毁函数

#include <pthread.h>
 
int pthread_cond_init(pthread_cond_t *restrict cond,                 pthread_condattr_t *restrict                                                                                           attr);
 
int pthread_cond_destroy(pthread_cond_t *cond);

 

Both return: 0 if OK, error number on failure

  2、获取条件变量的函数

#include <pthread.h>
 
int pthread_cond_wait(pthread_cond_t *restrict cond,
                      pthread_mutex_t *restrict      mutex);
 
int pthread_cond_timedwait(pthread_cond_t      *restrict cond,                           pthread_mutex_t      *restrict mutex, const struct timespec      *restrict timeout);

 

Both return: 0 if OK, error number on failure

 3、唤醒在条件变量上阻塞的函

#include <pthread.h>
 
int pthread_cond_signal(pthread_cond_t *cond);
 
int pthread_cond_broadcast(pthread_cond_t *cond);

 

Both return: 0 if OK, error number on failure

举例:

如果我们有一个不限制长度的队列,有多个线程向队列写入数据,有多个线程从队列读取数据。要求:1 同时只有一个线程操作对联。2如果队列没有数据,读取线程需要等待。3 写队列线程,写入数据后通知等待数据的线程。

我们可以用下面的代码实现:

队列使用的要点:

1、必须要用pthread_mutex_t保护队列

2、用条件变量实现,等待、通知的功能

3、条件等待的判断条件必须是while,防止spurious awaken.如get_msg函数中的红色部分。

 

在代码中,用蓝色注释,解释条件变量使用,发生的事情。

struct data_block * get_msg(struct message_queue *mq)

{

    struct data_block *db = NULL;

    pthread_mutex_lock(&mq->lock);//获取mq->lock锁  ,保护下面队列为空的判断 

    while(mb_empty(&mq->message_block))

         pthread_cond_wait(&mq->cond , &mq->lock);//释放mq->lock锁,获取mq->cond条件,线程阻塞。当写队列的线程发送signal信号的时候,获取mq->lock锁,释放mq->cond。  

    db = mb_pop_front(&mq->message_block);

 

    pthread_mutex_unlock(&mq->lock);//最后释放mq->lock锁

    return db;

}

 

int put_msg(struct message_queue *mq , struct data_block *db)

{

    pthread_mutex_lock(&mq->lock); //获取mq->lock锁  ,保护下面队列操作  

   int errn = mb_push_back(db , &mq->message_block);    pthread_metex_unlock(&mq->lock); //最后释放mq->lock锁

 pthread_cond_signal(&mq->cond);//通知阻塞在mq->cond的线程

    return errn;

}


http://www.niftyadmin.cn/n/1870214.html

相关文章

汇编器

汇编器的分类(the category of the assembler) 1、MASM&#xff08;Microsoft Assembler&#xff09;masm 6.0 是免费的。微软现在不再发行masm&#xff0c;此功能已经集成在visual studio 中。 2、MASM32是由Steve Hutchessen开发的。集成了masm原有功能和win32 API。 3、NASM…

What's the difference between glibc and libc6?

原文链接&#xff1a;http://www.sharkyforums.com/archive/index.php/t-42402.html Whats the difference between glibc and libc6?libc is the C library; basically, it contains all of the system functions that most (if not all) programs need to run on Linux. lib…

同包同名的类的时候,使用哪个,哪个优先

最近项目中&#xff0c;遇到了一个问题&#xff0c;起因是这个样子的&#xff1a; 项目A&#xff0c;它是一个web项目(使用spring开发&#xff0c;tomcat下部署&#xff0c;提供jsp页面&#xff0c;供用户使用)&#xff0c;会引入产品包&#xff0c;暂且用product.jar来代替产…

c++的POD数据类型

1.1 c的POD数据类型 POD: plain old data的缩写。 POD类型包括下面类型&#xff1a; 1、标量类型&#xff0c;c/c的基本类型 signed integer types (signed char, short, int, long),unsigned integer types (unsigned char, unsigned short, unsigned int, unsigned l…

Java系列笔记(3) - Java 内存区域和GC机制

目录 Java垃圾回收概况Java内存区域Java对象的访问方式Java内存分配机制Java GC机制垃圾收集器 Java垃圾回收概况 Java GC&#xff08;Garbage Collection&#xff0c;垃圾收集&#xff0c;垃圾回收&#xff09;机制&#xff0c;是Java与C/C的主要区别之一&#xff0c;作为Jav…

计算n位m进制所有整数的算法

算法来源&#xff1a;《the art of computer programming vol 4》 算法的思路&#xff1a; 1、 将n位整数记作a1a2…an &#xff0c;初始化为0…00。 2、 令 j n, 计算 aj aj 1; 当 aj 等于 m-1时&#xff0c;向高位进1&#xff0c;此位设为0&#xff0c;对所有为都…

奇偶校验算法

算法来源&#xff1a;互联网 功能描述:&#xff1a; 大家都知道&#xff0c;信息是以比特流的方式传输的&#xff0c;类似01000001。在传输过程中&#xff0c;有可能会发生错误&#xff0c;比如&#xff0c;我们存储了01000001&#xff0c;但是取出来却是01000000&#xff0c;…

Comparing Two High-Performance I/O Design Patterns

来至&#xff1a;http://www.artima.com/articles/io_design_patterns.html