Boost C++ Libraries: Ticket #8600: wait_for_any hangs, if called with multiple copies of shared_future referencing same task https://svn.boost.org/trac10/ticket/8600 <p> The following small test program shows the problem: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/thread.hpp&gt; int calculate_the_answer_to_life_the_universe_and_everything() { return 42; } int main(int argc, char* argv[]) { boost::packaged_task&lt;int&gt; pt(calculate_the_answer_to_life_the_universe_and_everything); boost::shared_future&lt;int&gt; fi1 = boost::shared_future&lt;int&gt;(pt.get_future()); boost::shared_future&lt;int&gt; fi2 = fi1; boost::thread task(boost::move(pt)); // launch task on a thread boost::wait_for_any(fi1, fi2); std::cout &lt;&lt; "Wait for any returned\n"; return (0); } </pre><p> This program hangs infinitely in the call to boost::wait_for_any. From the docs I would expect this to work, because it's allowed to copy shared_futures. If this is not allowed, a possibility would be needed, to find out, if two shared_futures point to the same task or not. Currently wait_for_any is unusable, if there are chances, that multiple shared_futures point to the same result. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8600 Trac 1.4.3 viboes Tue, 21 May 2013 17:44:34 GMT owner, status changed https://svn.boost.org/trac10/ticket/8600#comment:1 https://svn.boost.org/trac10/ticket/8600#comment:1 <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> <p> Thanks for catching this, even if I would not this to work. </p> <p> I think the fix would be to document that the shared state of the N-futures must be different as a pre-condition. </p> <p> I don't think the library must provide any check to avoid this usage. </p> Ticket Martin Apel <martin.apel@…> Wed, 22 May 2013 07:24:08 GMT <link>https://svn.boost.org/trac10/ticket/8600#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8600#comment:2</guid> <description> <p> That's fine with me, as long as there is a possibility to detect, if two shared_futures point to the same object. Currently there is no such function, so there is no possibility to remove the duplicates before calling wait_for_any. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 23 May 2013 21:59:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8600#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8600#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8600#comment:2" title="Comment 2">Martin Apel &lt;martin.apel@…&gt;</a>: </p> <blockquote class="citation"> <p> That's fine with me, as long as there is a possibility to detect, if two shared_futures point to the same object. Currently there is no such function, so there is no possibility to remove the duplicates before calling wait_for_any. </p> </blockquote> <p> I understand your concern. I will see what can be done. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 24 May 2013 19:58:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8600#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8600#comment:4</guid> <description> <p> What would you expect from the following code </p> <pre class="wiki">int main(int argc, char* argv[]) { boost::packaged_task&lt;int&gt; pt(calculate_the_answer_to_life_the_universe_and_everything); boost::future&lt;int&gt; fi1 = pt.get_future(); boost::thread task(boost::move(pt)); // launch task on a thread boost::wait_for_any(fi1, fi1); std::cout &lt;&lt; "Wait for any returned\n"; return (0); } </pre> </description> <category>Ticket</category> </item> <item> <author>Martin Apel <martin.apel@…></author> <pubDate>Mon, 27 May 2013 06:50:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8600#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8600#comment:5</guid> <description> <p> Interesting. I see that it's strange to explicitly pass two identical objects in there, but I would still expect it to wait correctly and not to silently hang infinitely. When called with iterators, this becomes more natural, because it's nothing special if a list contains the same item twice. </p> <p> I would be perfectly happy to avoid this situation, if there only was a possibility to detect his. I have now found a workaround for my application, where I pass additional data and filter out futures based on this additional data. The best approach for me would be, if there was an operator== for shared_future objects, so I can compare, if they point to the same object or not. This would not incur any overhead and still make my use case possible. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 04 Jun 2013 06:13:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8600#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8600#comment:6</guid> <description> <p> I have requested some advice to the std-proposal forum and when_all, when_any should work when the futures share the same associated shared state. I will need to rethink the algorithm. </p> </description> <category>Ticket</category> </item> </channel> </rss>