#4078 closed Bugs (invalid)
interrupt()ion of threads in timed_wait() of a condition variable has unexpected side effects
Reported by: | Owned by: | Anthony Williams | |
---|---|---|---|
Milestone: | Boost 1.43.0 | Component: | thread |
Version: | Boost 1.39.0 | Severity: | Problem |
Keywords: | Cc: |
Description
If multiple threads are waiting on a condition variable with timed_wait() then interrupt()ing one them makes timed_wait() return true
in another thread. Happens under Linux, not tested under Windows.
I haven't tested it with version later than 1.39.0 but couldn't find a ticket reporting that bug or any mention of it in the changelog.
#include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/condition_variable.hpp> #include <iostream> using namespace std; using namespace boost; condition_variable cv; mutex mut; void test_thread( const char* name ) { try { mutex::scoped_lock lock( mut ); cout << name << " timed_wait: " << cv.timed_wait( lock, posix_time::time_duration( 0, 20, 0 ) ) << endl; } catch( thread_interrupted& ) { cout << name << " interrupted" << endl; } } int main() { thread thread1( test_thread, "1" ); thread thread2( test_thread, "2" ); sleep( 1 ); cout << "intr 1" << endl; thread1.interrupt(); sleep( 1 ); cout << "intr 2" << endl; thread2.interrupt(); sleep( 1 ); thread1.join(); thread2.join(); return 0; }
Change History (2)
comment:1 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 13 years ago
Thanks for the answer. With this explanation in mind, the documentation and the code-blocks in "Effects" make more sense.
However, the only thing in the written documentation to suggest this behaviour are the words "or spuriously" for both wait() and timed_wait(). If you're aware of POSIX CV-semantics, it's understood. If you aren't (like I was), it's easy to miss this detail. Maybe it would be helpful to point this out more authoritatively.
This behavior is expected. Spurious wakeups are permitted for condition variables. This is one of the reasons why you usually need to wait in a loop.