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没有必要执行了,因为此之前没有对互斥量的加锁操作,所以就没有解锁的原子操作了。

 

 

 

你可能感兴趣的:(工作,null,Signal)