美文网首页
pthread_cond_wait条件变量的使用

pthread_cond_wait条件变量的使用

作者: 动感新势力fan | 来源:发表于2018-08-02 10:32 被阅读15次

pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,然后阻塞在等待队列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx); 再读取资源

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

int sequence=1;
int eat = 1;
pthread_mutex_t productor_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t my_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t productor_mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t my_cond1 = PTHREAD_COND_INITIALIZER;

void *productor1()
{
    int my_sequence = 1;

    while(1)
    {
        pthread_mutex_lock(&productor_mutex);
        while(sequence!=my_sequence)
        {
            pthread_cond_wait (&my_cond, &productor_mutex);
        }


        pthread_mutex_lock(&productor_mutex1);
        while(eat!=1)
        {
            pthread_cond_wait (&my_cond1, &productor_mutex1);
        }
        printf("A ");

        eat=0;
        pthread_cond_broadcast (&my_cond1);
        pthread_mutex_unlock (&productor_mutex1);

        sequence=2;
        pthread_cond_broadcast(&my_cond);
        pthread_mutex_unlock (&productor_mutex);
    }

    return 0;
}

void *productor2()
{
    int my_sequence=2;

    while(1)
    {
        pthread_mutex_lock(&productor_mutex);
        while(sequence!=my_sequence)
        {
            pthread_cond_wait (&my_cond, &productor_mutex);
    }

        pthread_mutex_lock(&productor_mutex1);
        while(eat!=1)
        {
        pthread_cond_wait (&my_cond1, &productor_mutex1);
        }
        printf("B ");

        eat=0;
        pthread_cond_broadcast (&my_cond1);
        pthread_mutex_unlock (&productor_mutex1);

        sequence=1;
        pthread_cond_broadcast (&my_cond);
        pthread_mutex_unlock (&productor_mutex);
    }

    return 0;
}

void *consumer()
{
    long a=200;

    while(a--)
    {
        pthread_mutex_lock(&productor_mutex1);
        while(eat!=0)
        {
            pthread_cond_wait (&my_cond1, &productor_mutex1);
        }
        printf("C ");

        eat=1;
        pthread_cond_broadcast (&my_cond1);
        pthread_mutex_unlock (&productor_mutex1);
    }

    return 0;
}

int main(void)
{
    pthread_t pth1=0,pth2=0,pth3=0;
    int err=-1;
    void *tret=NULL;

    err = pthread_create(&pth1,NULL,productor1,NULL);
    if(err)
    {
        printf("creat pthread productor failed!\n");
        exit(1);
    }
    err = pthread_create(&pth2,NULL,productor2,NULL);
    if(err)
    {
        printf("creat pthread productor failed!\n");
        exit(1);
    }

    err = pthread_create(&pth3,NULL,consumer,NULL);
    if(err)
    {
        printf("creat pthread consumer failed!\n");
        exit(1);
    }

    err = pthread_join(pth3,&tret);
    if(err)
        printf("can't join pthread consumer!\n");
    else
            printf("pthread consumer exit with %d!\n",(int)tret);

    pthread_cancel(pth1);
    pthread_cancel(pth2);
    exit(0);
}

相关文章

  • 条件变量中使用互斥锁的必要性

    简单介绍一下使用条件变量时,增加互斥锁的必要性。pthread_cond_wait的API如下 条件变量的用法就不...

  • pthread_cond_wait条件变量的使用

    pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,然后阻塞在等待队...

  • Linux系统编程—条件变量

    条件变量是用来等待线程而不是上锁的,条件变量通常和互斥锁一起使用。条件变量之所以要和互斥锁一起使用,主要是因为互斥...

  • APUE//线程同步3

    使用条件变量进行线程同步 barrier,屏障

  • 10.多线程并发拓展

    一、死锁1.条件: 互斥条件 请求和保持条件 不剥夺条件 环路等待条件 二、多线程并发最佳实践 使用本地变量 使用...

  • C++实现 生产者消费者模型

    condition_variable条件变量可以用来实现线程同步,它必须与互斥量mutex配合使用。条件变量适用场...

  • volatile的使用场景

    volatile的使用场景 使用volatile修饰的变量最好满足以下条件: 对变量的写操作不依赖于当前值 该变量...

  • c++11中的特性

    1、原子类型:atomic<> : 变量执行++操作是一个原子的操作2、条件变量的使用使用对象调用方法的形式...

  • shell-8 for

    for 语法一 for语法二C式的for命令 for循环使用多个变量 for 无限循环 使用((;;)) 条件可以...

  • infer

    作用:用来做类型推断 使用场景:声明类型变量需要和条件类型配合使用 限制1:只能在条件类型的 extends 子句...

网友评论

      本文标题:pthread_cond_wait条件变量的使用

      本文链接:https://www.haomeiwen.com/subject/dccidftx.html