Boost C++ Libraries: Ticket #4725: Synchronisation and shared mutex's Get total_count https://svn.boost.org/trac10/ticket/4725 <p> Hey there I wish to create what is in essence a barrier but with the capacity to flexibly dictate how many threads are in the barrier. The problem is synchronising. </p> <p> Here is an example of the problem that shows I need to get the total count of threads accessing the shared total_count variable atomically from the boost library. I would like to know if there is a reason this is not implemented. </p> <p> Here are some variables to demonstrate the issue. </p> <p> boost::condition_variable_any multiThreadLinkReady; </p> <p> boost::shared_mutex smutDependancyThreadCustomBarrierReady; </p> <p> bool boolIsSceneReady; </p> <p> <em>Run by a management thread </em></p> <p> void wakeUpIfAllReady() { </p> <blockquote> <p> mutAccessData.lock(); </p> </blockquote> <blockquote> <p> if (intThreadsCaughtCountPost &gt;= TOTALTHREADSLOADINGSCENE) { </p> <blockquote> <p> if (Here I need to the total count to also equal the Constant TOTALTHREADSLOADINGSCENE to garuntee every thread wakes up in a one time call to notify all of them into the next stage) </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> multiThreadLinkReady.notify_all(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> boolIsSceneReady = true; </p> </blockquote> <p> } </p> </blockquote> <p> </p> <blockquote> <p> mutAccessData.unlock(); </p> </blockquote> <p> } </p> <p> <em>Run by every thread but the management thread relating to this data </em></p> <p> void waitUntilAllReady() { </p> <blockquote> <p> mutAccessData.lock(); </p> </blockquote> <blockquote> <p> if (boolIsSceneReady == false) { </p> <blockquote> <p> intThreadsCaughtCountPost++; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> mutAccessData.unlock(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em>The thread is set to frozen to be awoken on the next line when the class is ready. It possible for this to not be set before the other thread calls notify all! multiThreadLinkReady.wait(smutDependancyThreadCustomBarrierReady); </em></p> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em>So we can reuse this on the next level loaded we need to decrement the counter. mutAccessData.lock(); </em></p> </blockquote> </blockquote> <blockquote> <blockquote> <p> intThreadsCaughtCountPost--; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> mutAccessData.unlock(); </p> </blockquote> <p> } else { </p> <blockquote> <p> mutAccessData.unlock(); </p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> There is no way of avoiding using interrupts when using the available barrier mechanism with a complex system involving alot of states this is not always desirable. If a thread were to fail and you needed to bail the other threads out of the barrier class you need to do alot more than if you can use a conditional variable like this and get the total threads that are actively waiting, creating a flexible barrier. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4725 Trac 1.4.3 dominicstreeter@… Sun, 10 Oct 2010 15:39:53 GMT <link>https://svn.boost.org/trac10/ticket/4725#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4725#comment:1</guid> <description> <p> I have written into the Boost file "condition_variable.hpp" a function to perform the correction. Details on this can be found easily here. </p> <p> <a class="ext-link" href="http://www.gamedev.net/community/forums/topic.asp?topic_id=584562"><span class="icon">​</span>http://www.gamedev.net/community/forums/topic.asp?topic_id=584562</a> </p> <p> Thank you for your time </p> <p> <a class="missing wiki">EnlightenedOne</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 27 Nov 2010 16:57:48 GMT</pubDate> <title>component changed https://svn.boost.org/trac10/ticket/4725#comment:2 https://svn.boost.org/trac10/ticket/4725#comment:2 <ul> <li><strong>component</strong> <span class="trac-field-old">threads</span> → <span class="trac-field-new">thread</span> </li> </ul> Ticket dominicstreeter@… Sat, 27 Nov 2010 18:13:14 GMT <link>https://svn.boost.org/trac10/ticket/4725#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4725#comment:3</guid> <description> <p> I wish to close this ticket on the grounds that the above code is flawed and a work around the problem faced here requires a seperate class to encapsulate a count of the total threads being used. Although there is no method for getting the total threads locked in a barrier from the class you can encapsulate this functionality in your own class outside of a barrier class and pass in the expected total threads to be waiting from there. </p> <p> To explain what the barrier class that encapsulates/replaces this mechanism actually ended up looking like here is a rough abstract of my working barrier. </p> <blockquote> <p> boost::mutex mutAccessData; </p> </blockquote> <blockquote> <p> boost::condition_variable_any multiThreadLink; </p> </blockquote> <blockquote> <p> int intThreadsCaughtCount; </p> </blockquote> <p> You pass in the number of threads registered to be waiting with the scene if they are all waiting on the barrier you wake them up and return true signalling success to a managing thread that might be waiting for all of them to be at the barrier. </p> <blockquote> <p> bool getWakeUpIfAllReady(int intThreadsHandledByObject) { </p> <blockquote> <p> bool boolIsAwoken; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> mutAccessData.lock(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> if (intThreadsCaughtCount == intThreadsHandledByObject) { </p> <blockquote> <p> multiThreadLink.notify_all(); </p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> boolIsAwoken = true; </p> </blockquote> <p> } else { </p> <blockquote> <p> boolIsAwoken = false; </p> </blockquote> <p> } </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> mutAccessData.unlock(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> return boolIsAwoken; </p> </blockquote> <p> } </p> </blockquote> <p> This is called by threads that need to wait until they are woken up. </p> <blockquote> <p> void waitUntilAllReady() { </p> <blockquote> <p> mutAccessData.lock(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> intThreadsCaughtCount++; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> multiThreadLink.wait(mutAccessData); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> intThreadsCaughtCount--; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> mutAccessData.unlock(); </p> </blockquote> <p> } </p> </blockquote> <p> There were one or two scenarios where the rewritten get total threads might be useful for handling complex state logic for a shutdown during an erroneous event; however even that use has alternate work arounds which I have used now. Therefore I wish to retract my ticket. </p> <p> Perhaps a boost class to encapsulate this behaviour might be of value? If not this would be a useful exercise to do for an example in the boost documentation on threading for newcomers. Anyway thank you for your time :) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 30 Dec 2011 23:17:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4725#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4725#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4725#comment:3" title="Comment 3">dominicstreeter@…</a>: </p> <blockquote class="citation"> <p> There were one or two scenarios where the rewritten get total threads might be useful for handling complex state logic for a shutdown during an erroneous event; however even that use has alternate work arounds which I have used now. Therefore I wish to retract my ticket. </p> <p> Perhaps a boost class to encapsulate this behaviour might be of value? If not this would be a useful exercise to do for an example in the boost documentation on threading for newcomers. Anyway thank you for your time :) </p> </blockquote> <p> Hi, </p> <p> It would be great if you may write the text to be added to the tutorial. </p> <p> Vicente </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 07 Jan 2012 23:46:45 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4725#comment:5 https://svn.boost.org/trac10/ticket/4725#comment:5 <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">worksforme</span> </li> </ul> <p> Closed as requested. </p> Ticket