Boost C++ Libraries: Ticket #8685: lockfree spsc_queue iterator pop failure https://svn.boost.org/trac10/ticket/8685 <p> In the case where the producer has populated with N items such that they wrap around the end of the ring buffer, and the consumer uses the pop() interface with an output iterator, the pop method splits the copying into two chunks. </p> <p> The problem is that both calls to std::copy use the same destination value. </p> <pre class="wiki"> size_t count0 = max_size - read_index; size_t count1 = avail - count0; std::copy(internal_buffer + read_index, internal_buffer + max_size, it); std::copy(internal_buffer, internal_buffer + count1, it); </pre><p> What we see is pop() returns 2 (in the case where N == 2), but what's read is the last value added, then some old value which happened to be there from before. The N-1 value is lost. </p> <p> Should the second call to std::copy be: </p> <pre class="wiki"> std::copy(internal_buffer, internal_buffer + count1, it + count0); </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8685 Trac 1.4.3 aubrey@… Mon, 10 Jun 2013 22:57:09 GMT <link>https://svn.boost.org/trac10/ticket/8685#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8685#comment:1</guid> <description> <p> I confirm the fix is working. </p> <p> Jason Aubrey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 11 Jun 2013 13:58:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8685#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8685#comment:2</guid> <description> <pre class="wiki">#include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; #include &lt;vector&gt; #include &lt;string&gt; using namespace std; template &lt;class Sequence, typename OutputIterator&gt; void test_copy(const Sequence &amp;seq, OutputIterator out) { size_t mid = seq.size() / 2; copy(begin(seq), begin(seq) + mid, out); copy(begin(seq) + mid, end(seq), out); } template &lt;class Sequence, typename OutputIterator&gt; void test_copy_fix(const Sequence &amp;seq, OutputIterator out) { size_t mid = seq.size() / 2; copy(begin(seq), begin(seq) + mid, out); copy(begin(seq) + mid, end(seq), out + mid); } template &lt;class Sequence&gt; void print_seq(const Sequence &amp;seq, const string&amp; name) { cout &lt;&lt; name &lt;&lt; ": "; copy(begin(seq), end(seq), ostream_iterator&lt;typename Sequence::value_type&gt;(cout, " ")); cout &lt;&lt; '\n'; } int main() { vector&lt;int&gt; source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; print_seq(source, "source"); vector&lt;int&gt; dest1(10); test_copy(source, dest1.begin()); print_seq(dest1, "dest1a"); fill(begin(dest1), end(dest1), 0); test_copy_fix(source, dest1.begin()); print_seq(dest1, "dest1b"); dest1.clear(); test_copy(source, back_inserter(dest1)); print_seq(dest1, "dest1c"); cout &lt;&lt; "ostream: "; test_copy(source, ostream_iterator&lt;int&gt;(cout, " ")); cout &lt;&lt; '\n'; // cout &lt;&lt; "ostream_b: "; // test_copy_fix(source, ostream_iterator&lt;int&gt;(cout, " ")); // cout &lt;&lt; '\n'; return 0; } </pre><p> Produces this output: </p> <pre class="wiki">source: 1 2 3 4 5 6 7 8 9 dest1a: 5 6 7 8 9 0 0 0 0 0 dest1b: 1 2 3 4 5 6 7 8 9 0 dest1c: 1 2 3 4 5 6 7 8 9 ostream: 1 2 3 4 5 6 7 8 9 </pre><p> The first "dest" was how I was using the spsc queue, and how I saw the "problem". </p> <p> If the commented out lines are enabled, the code won't compile. This looks like my mistake in my usage of the spsc queue and output iterators. </p> </description> <category>Ticket</category> </item> <item> <author>meyers@…</author> <pubDate>Tue, 11 Jun 2013 14:15:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8685#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8685#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8685#comment:2" title="Comment 2">anonymous</a>: </p> <p> and by 'anonymous' I mean me. </p> <p> Rob Meyers </p> </description> <category>Ticket</category> </item> <item> <dc:creator>timblechmann</dc:creator> <pubDate>Tue, 11 Jun 2013 15:33:40 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8685#comment:4 https://svn.boost.org/trac10/ticket/8685#comment:4 <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">fixed</span> </li> </ul> <p> already fixed in trunk and 1.54 </p> Ticket