Boost C++ Libraries: Ticket #9357: Request for queue::size_unsafe() https://svn.boost.org/trac10/ticket/9357 <p> Hello, </p> <p> I have been using the lockfree queue in something like the following way (illustrative purposes only; please forgive any errors): </p> <pre class="wiki">class MyObject; queue&lt;MyObject*&gt; Input, Output; void HandleObject(MyObject* obj) { // Process obj, then push it. ... assert(Output.bounded_push(obj)); } // Assume Input has been filled with some number of objects. Output.reserve_unsafe(SizeOfInput); // Consume objects in multi-threaded code. thread_group grp; for(int i = 0; i &lt; NUM_THREADS; i++) { grp.create_thread(bind(&amp;queue::consume_all, &amp;Input, &amp;HandleObject)); } grp.join_all(); </pre><p> The point is that I ensure in sequential code that the output queue has enough capacity to receive nodes from the input queue. </p> <p> However, there is currently no easy way to get the size of the input queue. The only way I've found is to pop all of its entries, count how many there are, and push them back into the input queue, like the rather ungainly (untested): </p> <pre class="wiki">template&lt;typename T&gt; size_type size(queue&lt;T&gt; q) { queue&lt;T&gt; qtemp; T temp; size_type s = q.consume_all(bind(&amp;queue::unsynchronized_push, &amp;qtemp)); qtemp.consume_all(bind(&amp;queue::unsynchronized_push, &amp;q)); return s; } </pre><p> Would it be possible, instead, to add member functions like </p> <pre class="wiki">size_type queue::size_unsafe() const; size_type stack::size_unsafe() const; size_type spsc_queue::size_unsafe() const; </pre><p> to simplify this sort of code? (By analogy with STL, capacity_unsafe might be useful to some people too, though I don't have a use-case to support this.) </p> <p> Thanks for this excellent addition to boost! </p> <p> Clayton Davis </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9357 Trac 1.4.3 timblechmann Sat, 09 Nov 2013 10:26:07 GMT <link>https://svn.boost.org/trac10/ticket/9357#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9357#comment:1</guid> <description> <p> tbo, i think it would be safer to embed an atomic counter in the user code, mainly because of the following reasons </p> <ul><li>size() cannot be accurate: the container might be changed from another thread before the result is used </li><li>inaccurately changing the size, there are two possibility: either increment the counter before pushing/after popping or after popping/before pushing. which one should be used? </li></ul><p> in order to keep the interface most robust, i think it is best to defer these design decisions to the user code. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>timblechmann</dc:creator> <pubDate>Sat, 07 Dec 2013 10:39:07 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9357#comment:2 https://svn.boost.org/trac10/ticket/9357#comment:2 <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">wontfix</span> </li> </ul> <p> won't fix it, because it will better be off in user code. </p> Ticket