Pthread同步 - pthread_cond_wait

时间:2012-02-07 06:55:32

标签: pthreads mutex race-condition

在我的功能中,它会达到

pthread_cond_wait(& cond_state,& b_state);

当发送信号并唤醒此线程时。是否会在调用Enter之前立即尝试锁定互斥锁?

    void Enter(int g, int timer){
     pthread_mutex_lock(&b_state);

     if (room.state == 2 || room.state == g)
     {
       pthread_mutex_unlock(&b_state);
       Leave();
     }
     else 
     {  
       pthread_cond_wait(&cond_state, &b_state);
       Enter(g, timer);         //Try to enter again
     }
    }

如果线程进入休眠状态,我遇到问题,一旦它被唤醒,它会在调用Enter后卡在mutex_lock上。

1 个答案:

答案 0 :(得分:2)

是的,pthread_cond_wait的联机帮助页说:

  

等待线程仅在另一个线程调用后解除阻塞   pthread_cond_signal(3),或pthread_cond_broadcast(3)与之相同   条件变量,,当前线程重新获取锁定   互斥