2017-11-16 5 views
0

私のコードでは、スレッド1が ジョブを完了し、mutexlockをリリースしたかどうかを確認するためにpthread_mutx_trylock()を使用していますか?か否か ?pthread_mutex_trylock()は他のロックが解放されなくなるまで使用します

スレッド2において
 pthread_mutex_lock(&sync_wait); 
    // Waiting for return type. 
    pthread_mutex_unlock(&sync_wait); 

:スレッド1では

while (pthread_mutex_trylock(&sync_wait) == 0) { 
      }; // Wait until other thread has lock 

    // Waiting till thread 1 sync wait lock has not released. 
    pthread_mutex_unlock(&sync_wait); 
+0

あるように、 '//いくつかの他のコードtype.'の復帰を待っている、またはそれだけコメント、およびロックとアンロック操作であります互いの隣で起こる? –

答えて

1

マニュアルページから

pthread_mutex_trylockの()関数は ミューテックスのロックならばゼロを返しますmutexによって参照されるオブジェクトが取得されます。それ以外の場合は、エラーを示す数値 が返されます。

// so this will loop forever once you aquire lock 
    while (pthread_mutex_trylock(&sync_wait) == 0) { 
       }; // Wait until other thread has lock 

編集:

コードのこのセクションでは、

あなたのシナリオを処理する必要があり
while (int ret = pthread_mutex_trylock(&sync_wait)) 
{ 
    // Unable to get Mutex probably some other thread aquired it 
    // sleep for some time usleep or even better use pthread_mutex_timedlock 
    // Ideally possible ret values should have been handled but for now 
    // this will do 
} 

とはい、一度の作業ここ

で行わpthread_mutex_unlock();もありmanpage ありますaq uestionはpthread_mutex_lockpthread_mutex_trylockhere の違いについて、これは本物のプログラムで別のexample of handling multiple return values from pthread_try_lock()

+0

スレッド1がロックを解除するまでスレッド1がロックを解除するまで待つ必要があります。 –

関連する問題