Boost C++ Libraries: Ticket #3960: condition_variable::timed_wait() cannot be interrupted https://svn.boost.org/trac10/ticket/3960 <p> I have a boost::condition_variable::timed_wait() with predicate functor overload in a thread. It's working fine, if the condition is satisfied it wakes up, and goes on in the execution. However, I'd like to have an option to terminate the thread at this waiting-point so I made an boost::thread::interrupt call on this running thread and before I remove the object I used the join() method to wait until the thread is really quits. I did not catch any boost::thread_interruption exception in the thread after I call the interrupt() method. I added an exception handler, with a simple return and a break point on it, but it was never hit. After some struggling I tried to send a notification to the condition variable (notify_one() method) and surprisingly it caught the exception mentioned above. This is clearly different behaviour as it was described in the documentation under the Thread <a class="missing wiki">Management/Predefined</a> Interruption Points section where it claims that the condition_variable::timed_wait() is supposed to be an interruption point which means it needs to wake up with calling only the interrupt() method. I also think that is a bug not a documentation issue since when I tried to replace the condition_variable::timed_wait() to a simple this_thread::sleep() call, the interrupt immediately made the thread wake up and catch the thread_interruption exception. </p> <p> I'm not sure is this part involved in the same issue but I had to remove the coped_lock in the predicate function because it caused a deadlock there. It is really weird because the one and only other location where I'm using that mutex is in the wait_for_players() method and that supposed to run in the same thread. The mutex in the predicate function is maybe pointless but I saw this solution in many sample code (and makes sense if the predicate function has a block somewhere inside but in my case it is not necessary). How is it possible that the predicate function gets the mutex as it is locked? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3960 Trac 1.4.3 gyula.gubacsi@… Thu, 25 Feb 2010 11:20:51 GMT attachment set https://svn.boost.org/trac10/ticket/3960 https://svn.boost.org/trac10/ticket/3960 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">sample.cc</span> </li> </ul> <p> Partial code </p> Ticket gyula.gubacsi@… Thu, 25 Feb 2010 11:29:13 GMT <link>https://svn.boost.org/trac10/ticket/3960#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3960#comment:1</guid> <description> <p> Sorry, that wasn't a timed_wait() but a wait(). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 21 May 2010 15:54:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3960#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3960#comment:2</guid> <description> <p> The difference is that as far as you have a timeout you will be wake up and the the interruption check point will be done. You can pool with a timed_wait with the granularity you want to achieve the behavior you want. Other than polling I don't see any other solution. And make the wait function pool on a default granularity will penalize the users that don't need to interrupt the thread. </p> <p> It is also true that the documentation let think that wait will be interrupted if a interrupts is signaled, but it does this only before and after the underlying wait. </p> <p> "The following functions are interruption points, which will throw boost::thread_interrupted if interruption <strong>(BEFORE CALLING)</strong> is enabled for the current thread, and interruption is requested for the current thread: " </p> <p> Maybe the library could add a interruptible wait with a specific granularity. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 21 May 2010 16:23:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3960#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3960#comment:3</guid> <description> <p> See also <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2330" title="#2330: Patches: thread::interrupt() can be lost if condition_variable::wait() in progress (closed: fixed)">ticket:2330</a> which gives an implementation that doesn't seems too expensive. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Anthony Williams</dc:creator> <pubDate>Mon, 07 Jun 2010 08:10:03 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3960#comment:4 https://svn.boost.org/trac10/ticket/3960#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">duplicate</span> </li> </ul> <p> Duplicate of 2330 </p> Ticket