2012-02-07 56 views
0

在我的功能一個點,將達到Pthread的同步 - 調用pthread_cond_wait

調用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 
    } 
    } 

我有一個問題,當如果一個線程睡覺去了,一旦它醒來,它會被卡在後調用的mutex_lock輸入。

回答

2

是的,pthread_cond_wait手冊頁說:

等待的線程疏導另一個線程調用後才 調用pthread_cond_signal(3),或調用pthread_cond_broadcast(3)具有相同 條件變量,和當前線程重新獲得鎖 互斥鎖