Boost C++ Libraries: Ticket #3628: condition.notify_one() does not wake up a thread that is in condition.wait() or condition.timed_wait() https://svn.boost.org/trac10/ticket/3628 <p> You can see in the attached project's <a class="missing wiki">TestBoostThread</a>.cpp file that in <a class="missing wiki">ThreadFuncWaiter</a>(), the thread is in timed_wait() call before it releases the mutex to let the <a class="missing wiki">ThreadFuncNotifier</a>() thread notify it. But inside notify_one(), most of the time it will choose not to notify any "waiter". The total_count class member is 0 when that happens. If I uncomment the three lines marked as WORKAROUND_ in <a class="missing wiki">TestBoostThread</a>.cpp, then the threads will be notified, but this is just a workaround. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3628 Trac 1.4.3 Shouaib Ahmed <ranashoaib@…> Mon, 16 Nov 2009 21:43:38 GMT attachment set https://svn.boost.org/trac10/ticket/3628 https://svn.boost.org/trac10/ticket/3628 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">TestBoostThread.zip</span> </li> </ul> Ticket Shouaib Ahmed <ranashoaib@…> Mon, 16 Nov 2009 21:47:06 GMT <link>https://svn.boost.org/trac10/ticket/3628#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:1</guid> <description> <p> Just wanted to mention that I am using Visual Studio 2008 on Windows XP. </p> <p> Replying to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3628" title="#3628: Bugs: condition.notify_one() does not wake up a thread that is in ... (closed: invalid)">Shouaib Ahmed &lt;ranashoaib@…&gt;</a>: </p> <blockquote class="citation"> <p> You can see in the attached project's <a class="missing wiki">TestBoostThread</a>.cpp file that in <a class="missing wiki">ThreadFuncWaiter</a>(), the thread is in timed_wait() call before it releases the mutex to let the <a class="missing wiki">ThreadFuncNotifier</a>() thread notify it. But inside notify_one(), most of the time it will choose not to notify any "waiter". The total_count class member is 0 when that happens. If I uncomment the three lines marked as WORKAROUND_ in <a class="missing wiki">TestBoostThread</a>.cpp, then the threads will be notified, but this is just a workaround. </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 20 Nov 2009 10:26:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3628#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:2</guid> <description> <p> Replying to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3628" title="#3628: Bugs: condition.notify_one() does not wake up a thread that is in ... (closed: invalid)">Shouaib Ahmed &lt;ranashoaib@…&gt;</a>: </p> <blockquote class="citation"> <p> You can see in the attached project's <a class="missing wiki">TestBoostThread</a>.cpp file that in <a class="missing wiki">ThreadFuncWaiter</a>(), the thread is in timed_wait() call before it releases the mutex to let the <a class="missing wiki">ThreadFuncNotifier</a>() thread notify it. But inside notify_one(), most of the time it will choose not to notify any "waiter". The total_count class member is 0 when that happens. If I uncomment the three lines marked as WORKAROUND_ in <a class="missing wiki">TestBoostThread</a>.cpp, then the threads will be notified, but this is just a workaround. </p> </blockquote> <p> Have you tried to protect with a mutex the theConditions variable? This variable is used by <a class="missing wiki">ThreadFuncNotifier</a>() and <a class="missing wiki">ThreadFuncWaiter</a>(). </p> </description> <category>Ticket</category> </item> <item> <author>Shouaib Ahmed <ranashoaib@…></author> <pubDate>Thu, 26 Nov 2009 14:36:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3628#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/3628#comment:2" title="Comment 2">viboes</a>: </p> <blockquote class="citation"> <p> Replying to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3628" title="#3628: Bugs: condition.notify_one() does not wake up a thread that is in ... (closed: invalid)">Shouaib Ahmed &lt;ranashoaib@…&gt;</a>: </p> <blockquote class="citation"> <p> You can see in the attached project's <a class="missing wiki">TestBoostThread</a>.cpp file that in <a class="missing wiki">ThreadFuncWaiter</a>(), the thread is in timed_wait() call before it releases the mutex to let the <a class="missing wiki">ThreadFuncNotifier</a>() thread notify it. But inside notify_one(), most of the time it will choose not to notify any "waiter". The total_count class member is 0 when that happens. If I uncomment the three lines marked as WORKAROUND_ in <a class="missing wiki">TestBoostThread</a>.cpp, then the threads will be notified, but this is just a workaround. </p> </blockquote> <p> Have you tried to protect with a mutex the theConditions variable? This variable is used by <a class="missing wiki">ThreadFuncNotifier</a>() and <a class="missing wiki">ThreadFuncWaiter</a>(). </p> </blockquote> <p> It is protected by a mutex. May be you overlooked that. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 27 Nov 2009 01:31:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3628#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/3628#comment:3" title="Comment 3">Shouaib Ahmed &lt;ranashoaib@…&gt;</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/3628#comment:2" title="Comment 2">viboes</a>: </p> <blockquote class="citation"> <p> Replying to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3628" title="#3628: Bugs: condition.notify_one() does not wake up a thread that is in ... (closed: invalid)">Shouaib Ahmed &lt;ranashoaib@…&gt;</a>: </p> <blockquote class="citation"> <p> You can see in the attached project's <a class="missing wiki">TestBoostThread</a>.cpp file that in <a class="missing wiki">ThreadFuncWaiter</a>(), the thread is in timed_wait() call before it releases the mutex to let the <a class="missing wiki">ThreadFuncNotifier</a>() thread notify it. But inside notify_one(), most of the time it will choose not to notify any "waiter". The total_count class member is 0 when that happens. If I uncomment the three lines marked as WORKAROUND_ in <a class="missing wiki">TestBoostThread</a>.cpp, then the threads will be notified, but this is just a workaround. </p> </blockquote> <p> Have you tried to protect with a mutex the theConditions variable? This variable is used by <a class="missing wiki">ThreadFuncNotifier</a>() and <a class="missing wiki">ThreadFuncWaiter</a>(). </p> </blockquote> <p> It is protected by a mutex. May be you overlooked that. </p> </blockquote> <p> You are right, I overlooked this mutex. </p> <p> To which total_count class member are you referring to? </p> <p> The explanation I found is that if you don't add the workaround, the thread <a class="missing wiki">ThreadFuncNotifier</a> will lock the mutex during the iteration through the theConditions container + 500 (miliseconds?). This left enough time to make the wait for 50 milliseconds to expire. IMO you should not protect the Sleep neither in the thread <a class="missing wiki">ThreadFuncNotifier</a> nor <a class="missing wiki">ThreadFuncWaiter</a>. </p> <p> The <em>WORKAROUND_ Sleep(50); seems no necessary to me. Does it works without? HTH. </em></p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 06 Jun 2010 12:47:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3628#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:5</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> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 07 Jan 2012 23:42:42 GMT</pubDate> <title>owner, status, milestone changed https://svn.boost.org/trac10/ticket/3628#comment:6 https://svn.boost.org/trac10/ticket/3628#comment:6 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.41.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> Ticket viboes Wed, 15 Aug 2012 22:09:32 GMT <link>https://svn.boost.org/trac10/ticket/3628#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:7</guid> <description> <p> Why do you need a recursive mutex? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 15 Aug 2012 22:21:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3628#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:8</guid> <description> <p> With </p> <pre class="wiki"> unsigned int i = 0; for (Conditions::iterator it = theConditions.begin(); it != theConditions.end() &amp;&amp; i &lt; 2; ++it) { (*it)-&gt;notify_one(); //WORKAROUND_ lockMtx.unlock(); //WORKAROUND_ Sleep(50); cout &lt;&lt; "Notified One " &lt;&lt; theConditions.size() &lt;&lt; endl; ++i; //WORKAROUND_ lockMtx.lock(); } </pre><p> only the two first conditions will be notified, so there will be up to 10 threads that will not be notified. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 17 Aug 2012 15:39:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3628#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3628#comment:9</guid> <description> <p> I think that I understand why it could occur that sometimes some threads are not notified. </p> <p> Both threads have a lock on theMutex. The underlying OS could choose to schedule the <a class="missing wiki">ThreadFuncNotifier</a> thread as many times as it wants. One of the threads <a class="missing wiki">ThreadFuncWaiter</a> ends by timeout as the <a class="missing wiki">ThreadFuncNotifier</a> is not signaling the specific condition. </p> <p> Let me know if you don't agree with my analysis. I will close the ticket as there is no evidence there is an issue on the library. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 18 Aug 2012 14:52:37 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3628#comment:10 https://svn.boost.org/trac10/ticket/3628#comment:10 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket