Boost C++ Libraries: Ticket #11317: boost unique_lock has no mutex https://svn.boost.org/trac10/ticket/11317 <p> This is an issue I am observing in the newly released boost_1_58 library. I suspect the problem may not be boost related, and could be an msvc 12.0 compiler related issue. The symptom I observe is that when a Visual C++ project TURNS ON one of the compiler optimization flags /Og, /O1, /O2 or /Ox Flags, the C++ code that I am using below is throwing an exception: </p> <p> Below is the sample code I use in the application: </p> <p> boost::mutex::scoped_lock &lt;Scoped Lock Variable&gt; (&lt;Mutex Object&gt;) </p> <p> Below is the exception message I am seeing which strangely occurs the second time a scoped lock is called on the same valid Mutex Object within the application: </p> <p> "boost unique_lock has no mutex" </p> <p> I am baffled by what causes this problem. When I turn off the optimization flags and execute the program, the application executes flawlessly. Only when I turn on optimization flags, this error consistently occurs. Workaround I am using is to disable optimization Flags for the release version. </p> <p> Below is my build configuration: </p> <p> Visual Studio community edition 2013, which uses msvc 12 compiler version; Windows 8.1; boost Library version 1.58 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11317 Trac 1.4.3 viboes Wed, 20 May 2015 06:03:05 GMT <link>https://svn.boost.org/trac10/ticket/11317#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11317#comment:1</guid> <description> <p> I would need more context, but I suspect that you are moving a unique_lock and then you are locking it. </p> <p> How optimization change the behavior of your application is out of my understanding. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 20 May 2015 06:04:33 GMT</pubDate> <title>status, component, type changed; owner set https://svn.boost.org/trac10/ticket/11317#comment:2 https://svn.boost.org/trac10/ticket/11317#comment:2 <ul> <li><strong>owner</strong> set 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>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">thread</span> </li> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Support Requests</span> </li> </ul> Ticket shyamps@… Wed, 20 May 2015 15:31:08 GMT <link>https://svn.boost.org/trac10/ticket/11317#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11317#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/11317#comment:1" title="Comment 1">viboes</a>: </p> <blockquote class="citation"> <p> I would need more context, but I suspect that you are moving a unique_lock and then you are locking it. </p> <p> How optimization change the behavior of your application is out of my understanding. </p> </blockquote> <p> vibeos, </p> <p> Heres a sample code snippet that can be used to reproduce this issue on msvc 12.0: </p> <p> #include &lt;boost/thread.hpp&gt; </p> <p> class <a class="missing wiki">MyClass</a> { </p> <blockquote> <p> boost::mutex myboostmutex; </p> </blockquote> <blockquote> <p> public: </p> </blockquote> <blockquote> <p> void Function1(); void Function2(); </p> </blockquote> <p> } </p> <p> void MyClass::Function1() { </p> <blockquote> <p> boost::mutex::scoped_lock <a class="missing wiki">SomeVariable</a> (myboostmutex); </p> </blockquote> <p> </p> <blockquote> <p> <em>Application code </em></p> </blockquote> <p> } </p> <p> void MyClass::Function2() { </p> <blockquote> <p> boost::mutex::scoped_lock <a class="missing wiki">SomeVariable</a> (myboostmutex); </p> </blockquote> <p> </p> <blockquote> <p> <em>Application code </em></p> </blockquote> <p> } </p> <p> To reproduce this bug, you need to follow these steps: </p> <p> 1) Compile code with one of the flags /Og, /O1, /O2 or /Ox Flags (TURNED ON) in msvc 12.0 compiler. 2) Create an instance of <a class="missing wiki">MyClass</a>; 3) First invoke Function1(), it should execute without any issue. 4) Next execute Function2(). </p> <p> When the code attempts to execute the boost::mutex::scoped_lock <a class="missing wiki">SomeVariable</a>(myboostmutex) line in Function2() You can see a runtime exception "boost unique_lock has no mutex". The same erroneous behaviour doesn't occur when you turn off all the above mentioned compiler optimization flags. </p> <p> I would recommend trying to compile boost 1_58 library on an older version msvc 10.0 or msvc 11.0 compiler. That can help isolate the problem to determine if this is related to the compiler version. </p> <p> Thanks, Shyam </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 20 May 2015 16:44:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11317#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11317#comment:4</guid> <description> <p> Hi, I would let you report this misbehavior to the compiler vendor and try with other versions of Boost or the compiler ;-) </p> <p> I don't see any problem with the Boost library here. </p> <p> Except if Nial wants to try, I would propose to close this ticket as invalid. </p> </description> <category>Ticket</category> </item> <item> <author>shyamps@…</author> <pubDate>Wed, 20 May 2015 16:59:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11317#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11317#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/11317#comment:4" title="Comment 4">viboes</a>: </p> <blockquote class="citation"> <p> Hi, I would let you report this misbehavior to the compiler vendor and try with other versions of Boost or the compiler ;-) </p> <p> I don't see any problem with the Boost library here. </p> <p> Except if Nial wants to try, I would propose to close this ticket as invalid. </p> </blockquote> <p> Hi, </p> <p> My two cents to this would be to hold off closing the ticket untill the code was tested with another version of the compiler by someone. The results I have gathered were from msvc 12.0 because that is the only compiler I have with me. </p> <p> -Shyam </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 20 May 2015 20:50:37 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/11317#comment:6 https://svn.boost.org/trac10/ticket/11317#comment:6 <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">wontfix</span> </li> </ul> <p> Unfortunately I have none </p> Ticket