Linux 之 条件变量

   日期:2020-09-14     浏览:172    评论:0    
核心提示:Linux 之 条件变量正文pthread_cond_init函数pthread_cond_destroy函数pthread_cond_wait函数pthread_cond_timedwait函数pthread_cond_signal函数pthread_cond_broadcast函数以上6 个函数的返回值都是:成功返回0, 失败直接返回错误号。pthread_cond_initint pthread_cond_init(pthread_cond_t *restrict cond, co

Linux 之 条件变量

正文

pthread_cond_init函数
pthread_cond_destroy函数
pthread_cond_wait函数
pthread_cond_timedwait函数
pthread_cond_signal函数
pthread_cond_broadcast函数
以上6 个函数的返回值都是:成功返回0, 失败直接返回错误号。

pthread_cond_init

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
参数
cond: pthread_cond_t 类型的条件变量
attr: 属性,默认传NULL
也可以使用静态初始化:
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

pthread_cond_destroy

int pthread_cond_destroy(pthread_cond_t *cond);

pthread_cond_wait

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
参数:
cond:条件变量
mutex:互斥量
在第一次调用之前先给mutex上锁,调用pthread_cond_wait函数会将锁释放,并阻塞直到被唤醒,被唤醒后对mutex上锁成功后返回,否则阻塞直到上锁成功。

pthread_cond_timedwait

int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);
参数
abstime:绝对时间
struct timespec {
time_t tv_sec; // seconds 秒
long tv_nsec; //nanosecondes 纳秒
}
阻塞超时后返回。

pthread_cond_signal

int pthread_cond_signal(pthread_cond_t *cond);
唤醒至少一个条件变量。

pthread_cond_broadcast

int pthread_cond_broadcast(pthread_cond_t *cond);
唤醒全部的条件变量。

条件变量实现生产者消费者模型

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#include<pthread.h>
#include<queue>
using namespace std;

pthread_mutex_t mutex;
pthread_cond_t cond;
queue<int>qu;

void str_error()
{ 
  const char * str = strerror(errno);
  write(STDERR_FILENO,str,sizeof(str));
  exit(-1);
}

void* fun1(void* arg)
{ 
    for(int i=0;i<1000;i++)
    { 
      pthread_mutex_lock(&mutex);
      qu.push(i);
      printf("producer pushed %d into queue\n",i);
      pthread_mutex_unlock(&mutex);
      pthread_cond_signal(&cond);
    }

    return NULL;
}

void* fun2(void* arg)
{ 
     while(1)
     { 
       pthread_mutex_lock(&mutex);
       if(qu.empty())
       { 
        pthread_cond_wait(&cond,&mutex);
       }
       int i = qu.front();
       qu.pop();
       printf("consumer poped %d from queue\n",i);
       pthread_mutex_unlock(&mutex);
     }
  return NULL;
}

int main()
{ 
    pthread_t producer,consumer;
    if(pthread_mutex_init(&mutex,NULL) != 0)
       str_error();
    if(pthread_cond_init(&cond,NULL) != 0)
       str_error();
    if(pthread_create(&producer,NULL,fun1,NULL) != 0)
       str_error();
    if(pthread_create(&consumer,NULL,fun2,NULL) != 0)
       str_error();
    pthread_join(producer,NULL);
    pthread_join(consumer,NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
}
                            
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服