奇妙な問題があります。私は次のコードを持っています:
dbg("condwait: timeout = %d, %d\n",
abs_timeout->tv_sec, abs_timeout->tv_nsec);
ret = pthread_cond_timedwait( &q->q_cond, &q->q_mtx, abs_timeout );
if (ret == ETIMEDOUT)
{
dbg("cond timed out\n");
return -ETIMEDOUT;
}
dbg
gettimeofday
すべての行の前に呼び出し、行の前に時間を追加します。その結果、次の出力が得られます。
7.991151: condwait: timeout = 5, 705032704
7.991158: cond timed out
ご覧のとおり、2つのデバッグ行の間を通過したのはわずか7マイクロ秒ですが、pthread_cond_timedwait
返されETIMEDOUT
ます。これはどのように起こりますか?cond変数を初期化するときに、時計を別のものに設定しようとしました。
int ret;
ret = pthread_condattr_init(&attributes);
if (ret != 0) printf("CONDATTR INIT FAILED: %d\n", ret);
ret = pthread_condattr_setclock(&attributes, CLOCK_REALTIME);
if (ret != 0) printf("SETCLOCK FAILED: %d\n", ret);
ret = pthread_cond_init( &q->q_cond, &attributes );
if (ret != 0) printf("COND INIT FAILED: %d\n", ret);
(エラーメッセージは印刷されません)。CLOCK_REALTIME
との両方を試しましCLOCK_MONOTONIC
た。
このコードはブロッキングキューの一部です。5秒以内にこのキューに何も入れられない場合、何か他のことが起こるような機能が必要です。を使用しない場合、ブロッキングキューは正常に機能するため、ミューテックスと条件は両方とも初期化されますpthread_cond_timedwait
。