IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 条件变量的用法示例 -> 正文阅读

[C++知识库]条件变量的用法示例

条件变量示例

条件变量最常用的场景是生产者和消费者模型

常见问题1:为什么条件变量要传入互斥锁

因为pthread_cond_wait里的操作会产生冲突(具体也不明白),调用前需要加锁,调用的过程中锁会释放,调用结束或又会上锁。总之锁传进去是会被操作的,先释放,被pthread_cond_signal激活后,又会上锁。

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

pthread_mutex_t mutex;
pthread_cond_t cond;

struct Node{
    int value;
    Node *next;
    Node(int val) {
        value = val;
        next  = NULL;
    }
};

Node *head;

static  void * producer(void *) {
    static int cnt = 0;
    while(1) {
        Node *tmp = new Node(cnt++);
        printf("producer lock\n");
        pthread_mutex_lock(&mutex);
        printf("producer produce food :%d\n", tmp->value);
        tmp->next = head;
        head      = tmp;
        printf("producer unlock\n");
        pthread_mutex_unlock(&mutex);
        printf("buy buy buy!!!\n");
        pthread_cond_signal(&cond);
        sleep(1);
    }
}

static void * comsumer(void *) {
    while(1) {
        printf("comsumer lock\n");
        pthread_mutex_lock(&mutex);
        while(head == NULL) {
            printf("comsumer wait...\n");
            pthread_cond_wait(&cond, &mutex);
        }
        printf("comsumer start eating\n");
        Node *tmp = head;
        printf("comsumer eating %d\n", head->value);
        head      = head->next;
        printf("comsumer unlock\n");
        pthread_mutex_unlock(&mutex);
        delete tmp;
    }
    return NULL;
}

int main(int argc, char ** argv) {
    pthread_t producedThread, comsumerThread;
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);
    pthread_create(&producedThread, NULL, producer, NULL);
    pthread_create(&comsumerThread, NULL, comsumer, NULL);
    pthread_join(producedThread, NULL);
    pthread_join(comsumerThread, NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-06 10:54:21  更:2022-05-06 10:55:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 4:49:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码