そもそも、私の問題は違います。
私のシナリオでは、条件変数を待機する待機中のスレッドが 1 つあります。シグナルスレッドは条件変数をシグナルします。
私のコードは
//Wating thread
//Lock the mutex_
//mutex_ is of pthread_mutex_t and is initialized.
result = pthread_mutex_lock(&mutex_);
assert(result == 0);
do {
//Wait on condition variable cvar_
//cva_ is of pthread_cond_t and is initialized.
result = pthread_cond_wait(&cvar_, &mutex_); //POINT 1
}while(result == 0 && !state_);
//Unlock the mutex_.
result = pthread_mutex_unlock(&mutex_);
//signalling thread
result = pthread_mutex_lock(&mutex_); //POINT 2
assert(result == 0);
state_ = 1;
result = pthread_mutex_unlock(&mutex_);
assert(result == 0);
//signals the condition variable.
pthread_cond_signal(&cvar_);
私のオペレーティング システムは Mac OS X 10.8 ですが、最小ターゲットは 10.6
です。1 つを除いて、ほとんどすべてのケースで問題なく動作しています。
特定のケースでは、POINT 1 である の後pthread_cond_wait
、mutex_ が待機状態に入ったときにロックが解除されていないことに気付きました。pthread_mutex_trylock
この場合、どちらが EBUSY を返すかで確認しました。これにより、シグナルスレッドが待機状態になり、最終的にデッドロックが発生します。
pthread_cond_wait
渡されたミューテックスのロックを解除しない条件を知りたいです。この問題の原因は何ですか?