Boost C++ Libraries: Ticket #9058: with_lock_guard function https://svn.boost.org/trac10/ticket/9058 <p> There is example of with_lock_guard function in documentation. Generalized implementation: </p> <pre class="wiki">template &lt;class Lockable, class Function, class... Args&gt; auto with_lock_guard( Lockable&amp; m, BOOST_FWD_REF(Function) f, BOOST_FWD_REF(Args)... args ) -&gt; decltype(f(args...)) { boost::lock_guard&lt;Lockable&gt; lock(m); return f(args...); } </pre><p> I can create patch, to what file should I add this? Or create new one? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9058 Trac 1.4.3 viboes Wed, 28 Aug 2013 20:39:59 GMT <link>https://svn.boost.org/trac10/ticket/9058#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:1</guid> <description> <p> A new file please. </p> </description> <category>Ticket</category> </item> <item> <author>ruslan_baratov@…</author> <pubDate>Sat, 31 Aug 2013 07:01:55 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9058 https://svn.boost.org/trac10/ticket/9058 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">with_lock_guard.hpp</span> </li> </ul> <p> implementation </p> Ticket ruslan_baratov@… Sat, 31 Aug 2013 07:02:15 GMT attachment set https://svn.boost.org/trac10/ticket/9058 https://svn.boost.org/trac10/ticket/9058 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">with_lock_guard_example.cpp</span> </li> </ul> <p> example </p> Ticket ruslan_baratov@… Sat, 31 Aug 2013 07:02:30 GMT attachment set https://svn.boost.org/trac10/ticket/9058 https://svn.boost.org/trac10/ticket/9058 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">with_lock_guard_usage.cpp</span> </li> </ul> <p> usage </p> Ticket ruslan_baratov@… Sat, 31 Aug 2013 07:05:14 GMT <link>https://svn.boost.org/trac10/ticket/9058#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:2</guid> <description> <p> version for compilers without c++11 variadic templates support is limited to 4 arguments and not working with lambdas (boost::result_of limitation). Tested on clang 3.2/5.0, gcc 4.7.3, msvc 2012. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 31 Aug 2013 08:55:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:2" title="Comment 2">ruslan_baratov@…</a>: </p> <blockquote class="citation"> <p> version for compilers without c++11 variadic templates support is limited to 4 arguments and not working with lambdas (boost::result_of limitation). Tested on clang 3.2/5.0, gcc 4.7.3, msvc 2012. </p> </blockquote> <p> Thanks for the patch. </p> <p> Why the non variadic version don't provides move semantics? </p> <p> Could you add a patch for the documentation? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 31 Aug 2013 10:10:02 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/9058#comment:4 https://svn.boost.org/trac10/ticket/9058#comment:4 <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> </ul> Ticket ruslan_baratov@… Sat, 31 Aug 2013 11:42:45 GMT attachment set https://svn.boost.org/trac10/ticket/9058 https://svn.boost.org/trac10/ticket/9058 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">with_lock_guard.2.hpp</span> </li> </ul> <p> update implementation: forward for compiler without c++11 variadic templates </p> Ticket ruslan_baratov@… Sat, 31 Aug 2013 11:43:05 GMT attachment set https://svn.boost.org/trac10/ticket/9058 https://svn.boost.org/trac10/ticket/9058 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">with_lock_guard_usage.2.cpp</span> </li> </ul> <p> usage update </p> Ticket ruslan_baratov@… Sat, 31 Aug 2013 11:44:13 GMT <link>https://svn.boost.org/trac10/ticket/9058#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> Why the non variadic version don't provides move semantics? </p> </blockquote> <p> I simply don't think about this combination, fixed. </p> </description> <category>Ticket</category> </item> <item> <author>ruslan_baratov@…</author> <pubDate>Sat, 31 Aug 2013 11:45:43 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> Could you add a patch for the documentation? </p> </blockquote> <p> I take a look. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 31 Aug 2013 11:54:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:7</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:5" title="Comment 5">ruslan_baratov@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> Why the non variadic version don't provides move semantics? </p> </blockquote> <p> I simply don't think about this combination, fixed. </p> </blockquote> <p> Func could also be Movable. </p> <p> Could you add some test with movable only types? </p> </description> <category>Ticket</category> </item> <item> <author>ruslan_baratov@…</author> <pubDate>Sat, 31 Aug 2013 12:21:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:7" title="Comment 7">viboes</a>: </p> <blockquote class="citation"> <p> Func could also be Movable. </p> </blockquote> <p> Not working for g++ without c++11 support (can't bind simple function): </p> <pre class="wiki">with_lock_guard_usage.cpp:42:44: error: no matching function for call to 'with_lock_guard(boost::mutex&amp;, void (&amp;)())' </pre><p> clang without c++11 support refuse to work with boost::bind </p> <pre class="wiki">boost/bind/mem_fn_template.hpp:151:29: error: call to pointer to member function of type 'int (int &amp;)' drops 'const' qualifier </pre><blockquote class="citation"> <p> Could you add some test with movable only types? </p> </blockquote> <p> Ok. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 31 Aug 2013 13:06:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:9</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:8" title="Comment 8">ruslan_baratov@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:7" title="Comment 7">viboes</a>: </p> <blockquote class="citation"> <p> Func could also be Movable. </p> </blockquote> <p> Not working for g++ without c++11 support (can't bind simple function): </p> <pre class="wiki">with_lock_guard_usage.cpp:42:44: error: no matching function for call to 'with_lock_guard(boost::mutex&amp;, void (&amp;)())' </pre><p> clang without c++11 support refuse to work with boost::bind </p> <pre class="wiki">boost/bind/mem_fn_template.hpp:151:29: error: call to pointer to member function of type 'int (int &amp;)' drops 'const' qualifier </pre></blockquote> <p> I didn't said that it would be simple ;-) </p> <p> You would need to add some specific overloads when rvalue references are not supported by the compiler :( </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 01 Sep 2013 07:06:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:10</guid> <description> <p> The C++11 definition must be protected also with no rvalue references and no decltype. </p> </description> <category>Ticket</category> </item> <item> <author>ruslan_baratov@…</author> <pubDate>Sun, 01 Sep 2013 10:56:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:11</guid> <description> <p> Updated version on github: </p> <pre class="wiki"> github.com/ruslo/thread/commit/ea30f0dc6b1fe7951d1d0dff88fd03e946429669 </pre><p> For function without c++11 variadic templates, Func parameter is now movable: </p> <pre class="wiki">template &lt;class Lockable, class Func, class Arg&gt; typename boost::result_of&lt;Func(Arg)&gt;::type with_lock_guard( Lockable&amp; m, BOOST_FWD_REF(Func) func, BOOST_FWD_REF(Arg) arg ) { boost::lock_guard&lt;Lockable&gt; lock(m); return func( boost::forward&lt;Arg&gt;(arg) ); } </pre><p> overloaded version for function pointers: </p> <pre class="wiki">template &lt;class Lockable, class Func, class Arg1, class Arg2&gt; typename boost::result_of&lt; typename boost::add_pointer&lt;Func&gt;::type(Arg1, Arg2) &gt;::type with_lock_guard( Lockable&amp; m, Func* func, BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2 ) { BOOST_STATIC_ASSERT(boost::is_function&lt;Func&gt;::value); boost::lock_guard&lt;Lockable&gt; lock(m); return func( boost::forward&lt;Arg1&gt;(arg1), boost::forward&lt;Arg2&gt;(arg2) ); } </pre><p> In this version boost::bind which call non-const class method still not working (for variadic template version is OK). It's because BOOST_FWD_REF(Func) = const Func&amp;. I can't figure out how to do a workaround in not ugly way. I can make version with copy-by-value for types with T::result_type defined if this is appropriate. </p> <p> If used with lambda, BOOST_RESULT_OF_USE_DECLTYPE may need to be defined, see this bug: <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/7311"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/7311</a> </p> <p> I've added test: </p> <pre class="wiki"> test_with_lock_guard_bind.cpp test_with_lock_guard_lambda.cpp test_with_lock_guard_move.cpp test_with_lock_guard_simple.cpp </pre><p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:10" title="Comment 10">viboes</a>: </p> <blockquote class="citation"> <p> The C++11 definition must be protected also with no rvalue references and no decltype. </p> </blockquote> <p> I've add BOOST_NO_CXX11_DECLTYPE check; do BOOST_NO_CXX11_RVALUE_REFERENCES check really needed? I thought it automatically checked in BOOST_FWD_REF (?) </p> </description> <category>Ticket</category> </item> <item> <author>ruslan_baratov@…</author> <pubDate>Mon, 02 Sep 2013 20:12:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:12</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:6" title="Comment 6">ruslan_baratov@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> Could you add a patch for the documentation? </p> </blockquote> <p> I take a look. </p> </blockquote> <p> Documentation for with_lock_guard added to mutex_concept.qbk, examples updated in sync_tutorial.qbk. </p> <p> github.com/ruslo/thread/commit/425efc8ab1f87af30f16368ea105f82c41e6639d </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 23 Sep 2013 16:48:55 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/9058#comment:13 https://svn.boost.org/trac10/ticket/9058#comment:13 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.56.0</span> </li> </ul> Ticket viboes Tue, 15 Oct 2013 22:23:36 GMT <link>https://svn.boost.org/trac10/ticket/9058#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:14</guid> <description> <p> Hi, </p> <p> could you provide a complete patch to be added on next release? </p> <p> Vicente </p> </description> <category>Ticket</category> </item> <item> <author>ruslan_baratov@…</author> <pubDate>Thu, 17 Oct 2013 12:55:15 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:15</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:14" title="Comment 14">viboes</a>: </p> <blockquote class="citation"> <p> could you provide a complete patch to be added on next release? </p> </blockquote> <p> Ok, patch can be found here: </p> <pre class="wiki">github.com/ruslo/thread/commit/0d16fb0b97bd5218d4b694d574ccbd5cc668be9b </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 17 Oct 2013 17:46:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:16 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:16</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:15" title="Comment 15">ruslan_baratov@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:14" title="Comment 14">viboes</a>: </p> <blockquote class="citation"> <p> could you provide a complete patch to be added on next release? </p> </blockquote> <p> Ok, patch can be found here: </p> <pre class="wiki">github.com/ruslo/thread/commit/0d16fb0b97bd5218d4b694d574ccbd5cc668be9b </pre></blockquote> <p> Thanks, Vicente </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 28 Jan 2014 21:17:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:17 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:17</guid> <description> <p> Hi, </p> <p> I have started to integrate the patch, but I have some remarks. </p> <p> The patch doesn't allows to pass class member functions. It will take some time to take in account this feature that makes it uniform to the other callable interfaces as thread constructor, packaged_task constructor and async (at least for C++11 compilers). I will come back to you as soon as I have something working. </p> <p> Sorry for the delay. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 28 Jan 2014 21:17:59 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/9058#comment:18 https://svn.boost.org/trac10/ticket/9058#comment:18 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.56.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> Ticket ruslan_baratov@… Wed, 29 Jan 2014 16:02:45 GMT <link>https://svn.boost.org/trac10/ticket/9058#comment:19 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:19</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:17" title="Comment 17">viboes</a>: </p> <blockquote class="citation"> <p> Hi, </p> <p> I have started to integrate the patch, but I have some remarks. </p> <p> The patch doesn't allows to pass class member functions. It will take some time to take in account this feature that makes it uniform to the other callable interfaces as thread constructor, packaged_task constructor and async (at least for C++11 compilers). I will come back to you as soon as I have something working. </p> <p> Sorry for the delay. </p> </blockquote> <p> Hi Vicente! </p> <p> Looks like 'invoke' function fits perfectly for this job. I've found implementation in 'thread/detail/invoke.hpp' but I don't know what is the current ready-status of this file. Can I use it? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 29 Jan 2014 18:38:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9058#comment:20 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:20</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:19" title="Comment 19">ruslan_baratov@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9058#comment:17" title="Comment 17">viboes</a>: </p> <blockquote class="citation"> <p> Hi, </p> <p> I have started to integrate the patch, but I have some remarks. </p> <p> The patch doesn't allows to pass class member functions. It will take some time to take in account this feature that makes it uniform to the other callable interfaces as thread constructor, packaged_task constructor and async (at least for C++11 compilers). I will come back to you as soon as I have something working. </p> <p> Sorry for the delay. </p> </blockquote> <p> Hi Vicente! </p> <p> Looks like 'invoke' function fits perfectly for this job. I've found implementation in 'thread/detail/invoke.hpp' but I don't know what is the current ready-status of this file. Can I use it? </p> </blockquote> <p> Yes, the idea is to use invoke, but I have had some regressions recently with invoke on C++11 on some tests associated to async. When I disable the C++ implementation these test work. </p> <p> I don't know yet if I will deliver all the new features associated to futures as executors, ... in boost 1.56 :( </p> <p> I need to add unit test for the invoke function. </p> <p> Maybe you could give it a try and report to me any issues privately. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 09 Feb 2014 02:56:03 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/9058#comment:21 https://svn.boost.org/trac10/ticket/9058#comment:21 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.56.0</span> </li> </ul> <p> Pulled on develop <a class="ext-link" href="https://github.com/boostorg/thread/commit/b1ac899f23f915c19dfd3e8be9dd8b94b43ef55d"><span class="icon">​</span>https://github.com/boostorg/thread/commit/b1ac899f23f915c19dfd3e8be9dd8b94b43ef55d</a> and <a class="ext-link" href="https://github.com/boostorg/thread/commit/80634ee51e3a9fd39d1daae6d9431588f783a6ea"><span class="icon">​</span>https://github.com/boostorg/thread/commit/80634ee51e3a9fd39d1daae6d9431588f783a6ea</a> </p> Ticket viboes Sat, 15 Feb 2014 16:21:31 GMT <link>https://svn.boost.org/trac10/ticket/9058#comment:22 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9058#comment:22</guid> <description> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/750c849b0f0dff79a289111955260a4147ac7f59"><span class="icon">​</span>https://github.com/boostorg/thread/commit/750c849b0f0dff79a289111955260a4147ac7f59</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 15 Feb 2014 16:21:39 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9058#comment:23 https://svn.boost.org/trac10/ticket/9058#comment:23 <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">fixed</span> </li> </ul> Ticket