From: Yusuke ENDOH Date: 2010-05-06T21:30:28+09:00 Subject: [ruby-dev:41194] Re: MonitorMixin::ConditionVariable#wait timeout 遠藤です。 2010年5月6日20:58 Tanaka Akira : > たとえ wait が ETIMEDOUT で返って来たとしても、 > signal されていないことは保証されないのではないかと思うんですがどうでしょう? > > wait がタイムアウトで諦めた後、mutex をロックする前に、 > 他のスレッドにコンテキストスイッチして signal が起こることは無いんでしょうか。 少なくとも pthread では保証されていません。例の pthread の RATIONALE にも Similarly, when pthread_cond_timedwait() returns with the timeout error, the associated predicate may be true due to an unavoidable race between the expiration of the timeout and the predicate state change. The application needs to recheck the predicate on any return because it cannot be sure there is another thread waiting on the thread to handle the signal, and if there is not then the signal is lost. The burden is on the application to check the predicate. と、ETIMEOUT かどうかに関わらず predicate はチェックしろ、とあります。 > もし有るとすると、ETIMEDOUT でリトライすると、寝てしまって永久に起きない、 > という上記で懸念されている状況に結局なり得るのではないかと思うんですが。 このように ETIMEOUT は CV#wait の誤った使い方を推奨しかねない物なので、 私も #2629 で最初は反対していました。それでも「ないとまずい」ことがない のかどうか自信がありません。自信がないなら、pthread_cond_timedwait に 倣って返しておくのがいいかな、と思います。 ほんとスレッドは難しいですね。 -- Yusuke Endoh