首页 > 其他分享 >条件变量

条件变量

时间:2023-02-23 20:12:24浏览次数:31  
标签:变量 idx int 条件 mutex pthread cond NULL

介绍

#include <pthread.h>
pthread_cond_t cond;

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

功能:初始化/销毁条件变量

参数:

  • cond:条件变量的地址
  • attr:条件变量属性,一般使用默认属性,指定为 NULL

返回值:

  • 成功返回 0
  • 失败返回错误号
#include <pthread.h>

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

功能:阻塞调用该函数的线程

  • pthread_cond_wait 在阻塞线程中,如果已经对互斥锁上锁,为避免死锁会把锁打开
  • 当线程解除阻塞的时候,函数内部会帮助这个线程再次将互斥锁锁上,继续向下访问临界区

参数:

  • cond:条件变量的地址
  • mutex:互斥锁的地址

返回值:

  • 成功返回 0
  • 失败返回错误号
#include <pthread.h>

int pthread_cond_signal(pthread_cond_t *cond); // 唤醒至少一个

int pthread_cond_broadcast(pthread_cond_t *cond); // 唤醒所有

功能:唤醒阻塞在条件变量上的线程

参数:

  • cond:条件变量的地址

返回值:

  • 成功返回 0
  • 失败返回错误号

简单使用

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#define N 30

pthread_mutex_t mutex;
pthread_cond_t empty, full;

int buf[N], idx = -1;

void * producer(void *arg)
{
    while (1) {
        pthread_mutex_lock(&mutex);
        while (idx == N - 1) {
            pthread_cond_wait(&empty, &mutex);
        }
        buf[++idx] = 1;
        printf("producer offer %d\n", idx);
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&full);
        sleep(1);
    }
    
    pthread_exit(NULL);
}

void * consumer(void *arg)
{
    while (1) {
        pthread_mutex_lock(&mutex);
        while (idx == -1) {
            pthread_cond_wait(&full, &mutex);
        }
        printf("consumer get %d\n", idx);
        buf[idx--] = 0;
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&empty);
        sleep(3);
    }
    
    pthread_exit(NULL);
}

int main()
{
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&empty, NULL);
    pthread_cond_init(&full, NULL);
    
    pthread_t t1[5], t2[5];
    for (int i = 0; i < 5; i++) {
        pthread_create(&t1[i], NULL, producer, NULL);
        pthread_create(&t2[i], NULL, consumer, NULL);
    }
    for (int i = 0; i < 5; i++) {
        pthread_join(t1[i], NULL);
        pthread_join(t2[i], NULL);
    }
    
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&empty);
    pthread_cond_destroy(&full);
    
    return 0;
}

注意事项

标签:变量,idx,int,条件,mutex,pthread,cond,NULL
From: https://www.cnblogs.com/cong0221/p/17149226.html

相关文章

  • SQLite-安装,环境变量添加
    今后的Android编程中将使用SQLite,于是乎我们先要把它安装上。SQLite是什么?SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎。......
  • C#中Linq查询条件动态化
    由于Linq查询主要是强类型查询,所以很难做到像sql语句一样,在执行前可以动态拼接。不过通过方法或者集合方式也可以实现一定的动态化。1.动态的or操作。可以简单的使用list......
  • 环境变量
    一、环境变量1、pstree进程树可以看到我们在的进程pstree在bash的下面是他的子进程图中可以看到我们是在bash-bash下面使用的pstree说明在此窗口之前我们又创建了一个bash......
  • 不再讨厌系统环境变量了
    今天之前,我是痛恨环境变量的,痛恨到什么地步呢?就是我在学校的时候,我们班没几个同学听课,全班一半的JDK+eclipse环境是我配置的...起因我不再讨厌环境变量的原......
  • STATA修改变量名
    //修改变量名rename姓名namerename考号kaohaorename班级banji//所有变量名为大写rename*,upper//变量名kaohao为大写renamekaohao,upper//所有变量名为小写renam......
  • awk内置变量
    内置变量表:内置变量含义$0整行内容$1-$n当前行的第1-第n个字段内容NF当前行的字段个数,也就是列数NR当前行行号FNR多文件处理时,行号,但是每个文件......
  • Odoo 根据表单条件隐藏明细字段创建和删除按钮 one2many字段创建和删除按钮
    版本:odoo14需求:根据表单模型状态,明细行有条件的创建和删除eg:仅能在草稿下新增/在确认状态下删除ps:视图全程不需要可以在form/tree标签内指定create/delete......
  • pandas条件替换值(where&mask)
    pandas条件替换值(where&mask)在日常分析中,经常会遇到对数据的筛选处理相关的工作,我们可以使用loc和iloc定位分析筛选的列或行数据,下面介绍一种高级筛选的用法where和mask......
  • shell中的变量
    Shell中变量命名法则不能使程序中的保留字:例如if,for只能使用数字、字母及下划线,且不能以数字开头见名知义统一命名规则:驼峰命名法Shell中命名建议规则变量名......
  • JavaScript 字面量和变量
    <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <scripttype="text/javascript"> /* *字面量,都是一些不可改变的值 * ......