pthread_cond_wait 用法

int pthread_cond_wait(pthread_cont_t *restrict cond, pthread_mutex_t *restrict mutex); 函数的使用方法:

使用pthread_cond_wait等待条件变为真,如果在给定的时间内条件不能满足,那么会生成一个代表出错码的返回变量。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。函数把调用线程放到等待条件的线性列表上,然后对互斥量解锁,这两个操作是原子操作。这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道,这样线程就不会错过条件的任何变化。pthread_cond_wait返回时,互斥量再次被锁住。

 

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


#define  FAILURE  0
#define  SUCCESS  1
#define  UNS32  unsigned int
#define m_ERROR(format, args...)   printf(format, ## args);//fflush(stdout);

 

static pthread_t   g_pthread_wait;
static pthread_t   g_pthread_signal;
static pthread_t   g_pthread_mt;
pthread_mutex_t         g_pthread_mutex;
pthread_cond_t           g_pthread_cond;

 

UNS32 pthread_initialize_cond(void){
   if ( (pthread_cond_init(&g_pthread_cond, NULL)) != 0 ) {
    m_ERROR("/t!!! ERROR : pthread_cond_init !!!/n");
    return FAILURE;
   } 
  return SUCCESS;
}

 

UNS32 pthread_initialize_mutex(void){
     if ( (pthread_mutex_init(&g_pthread_mutex, NULL)) != 0 ) {
      m_ERROR("/t!!! ERROR : pthread_mutex_init !!!/n");
   return FAILURE;
     } 
  return SUCCESS;
}

 

/*
UNS32 pthread_destroy_cond(void){
   if ( (pthread_cond_destroy(&g_pthread_cond)) != 0 ) {
         m_ERROR("/t!!! ERROR : pthread_cond_destroy !!!/n");
         return FAILURE;
   }
         return SUCCESS;
}
*/

 

void pthread_mt(void * args) {
     pthread_mutex_lock(&g_pthread_mutex);
     printf("pthread_mt!! !/n");
     //pthread_mutex_unlock(&g_pthread_mutex);
}
 
void pthread_wait(void *args){
       pthread_detach(pthread_self());
        printf("pthread_wait_in/n");
        pthread_mutex_lock(&g_pthread_mutex);
       //pthread_mutex_trylock(&g_pthread_mutex);
        printf("pthread_wait_out/n");
        int i, j;
        for (i=0; i<3; i++){
            printf("pthread_cond_wait in/n");
            pthread_cond_wait(&g_pthread_cond, &g_pthread_mutex);
            printf("pthread_cond_wair out/n");
            printf("pthread_cond_wait: in1/n");
        }  
        printf("pthread_cond_wait: out1/n");
        pthread_mutex_unlock(&g_pthread_mutex);
        pthread_mutex_lock(&g_pthread_mutex);
        for (j=0; j<3; j++){
              pthread_cond_wait(&g_pthread_cond, &g_pthread_mutex);
              printf("pthread_cond_wait: in2/n");
        }
        printf("pthread_cond_wait: out2/n");
        pthread_mutex_unlock(&g_pthread_mutex);
        pthread_exit(NULL);

}

 

 void pthread_signal(void *args){
     pthread_detach(pthread_self());
     while (1) {
        sleep(3);
        pthread_mutex_lock(&g_pthread_mutex);
        pthread_cond_signal(&g_pthread_cond);
        pthread_mutex_unlock(&g_pthread_mutex);
     }
     pthread_exit(NULL);
}

 

int main() {
     pthread_initialize_cond();
     pthread_initialize_mutex();
 /*
     if (pthread_create(&g_pthread_mt , NULL, (void * (*)(void *)) pthread_mt, NULL) !=0) {
         return FAILURE;
     }

*/

     if (pthread_create(&g_pthread_wait , NULL, (void * (*)(void *)) pthread_wait, NULL) !=0) {
        return FAILURE;
     }
 
     if (pthread_create(&g_pthread_signal , NULL, (void * (*)(void *)) pthread_signal, NULL) !=0) {
        return FAILURE;
     }
     while(1);
}

一、

上段代码执行结果为:

pthread_wait_in
pthread_wait_out
pthread_cond_wait in
pthread_cond_wair out
pthread_cond_wait: in1
pthread_cond_wait in
pthread_cond_wair out
pthread_cond_wait: in1
pthread_cond_wait in
pthread_cond_wair out
pthread_cond_wait: in1
pthread_cond_wait: out1
pthread_cond_wait: in2
pthread_cond_wait: in2
pthread_cond_wait: in2
pthread_cond_wait: out2
可以看出pthread_cond_wait函数的作用。

 

二、

如果将红色的代码加进来,在将绿色的部分换成

        //pthread_mutex_lock(&g_pthread_mutex);
        pthread_mutex_trylock(&g_pthread_mutex);

非阻塞的锁,则pthread_cond_wait也可以工作,有相同的输出结果。

(因为虽然pthread_mt线程加锁了,但是 pthread_mutex_trylock为非阻塞加锁,所以可以继续执行,执行到pthread_cond_wait,进行解锁的原子操作操作时,就将pthread_mt线程加的锁解开了。)

 

 

三、

即使pthread_cond_wait函数前没有pthread_mutex_lock(&g_pthread_mutex);
加锁操作,pthread_cond_wait仍然可以工作,可以理解为黄色说明部分的原子操作2没有必要执行了,因为此之前没有对互斥量的加锁操作,所以就没有解锁的原子操作了。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值