Boost C++ Libraries: Ticket #12394: boost::lockfree::queue with gcc-4.6 + optimization https://svn.boost.org/trac10/ticket/12394 <p> I am seeing a potential issue with lockfree queue when using gcc-4.6 with compilation optimization turned on. </p> <p> The behavior i'm seeing is that call to push() which returns true does not guarantee that the pushed element is consumable immediately. (i.e. a subsequent call to pop() might return false. after further debugging it seems like the element _will become_ consumable but it might take few more cpu cycles till this happens. this issue is not happening when turning the compiler optimizations off or when using gcc-4.8. </p> <p> here is the test program i am using to reproduce the issue, which is spawning 2 threads, each of them looping through push() and then pop(), and at one point, the call to pop() fails which should never fail. </p> <p> compilation line: </p> <pre class="wiki">g++-4.6 -O1 -g -fno-inline -pthread -m64 -I/usr/local/include -c lockfree_test.cpp -o lockfree_test.cpp.o </pre><p> sample program: </p> <pre class="wiki">struct SemTask { int x; }; boost::lockfree::queue&lt;SemTask * &gt; queue(1); void* runner(void* arg) { std::cout &lt;&lt; '.' &lt;&lt; std::endl; while (!t_exit) { SemTask * task = new SemTask(); assert(queue.push(task)); assert(queue.pop(task)); delete task; } return NULL; } int main(int argc, char** argv) { assert(queue.is_lock_free()); #define NUM_THRD 2 pthread_t inc_x_thread[NUM_THRD]; for (int i = 0; i &lt; NUM_THRD; ++i) { if(pthread_create(&amp;inc_x_thread[i], NULL, runner, NULL)) { fprintf(stderr, "Error creating thread\n"); return -1; } } for (int i = 0; i &lt; NUM_THRD; ++i) { if(pthread_join(inc_x_thread[i], NULL)) { fprintf(stderr, "Error joining thread\n"); return -1; } } return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12394 Trac 1.4.3 anonymous Mon, 15 Aug 2016 15:23:45 GMT <link>https://svn.boost.org/trac10/ticket/12394#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12394#comment:1</guid> <description> <p> minor compilation issue in the test program. here is updated version: </p> <pre class="wiki">#include &lt;string&gt; #include &lt;deque&gt; #include &lt;cstdio&gt; #include &lt;boost/lockfree/queue.hpp&gt; #include &lt;cassert&gt; #include &lt;iostream&gt; struct SemTask { int x; }; boost::lockfree::queue&lt;SemTask * &gt; queue(1); void* runner(void* arg) { std::cout &lt;&lt; '.' &lt;&lt; std::endl; while (true) { SemTask * task = new SemTask(); assert(queue.push(task)); assert(queue.pop(task)); delete task; } return NULL; } int main(int argc, char** argv) { assert(queue.is_lock_free()); #define NUM_THRD 2 pthread_t inc_x_thread[NUM_THRD]; for (int i = 0; i &lt; NUM_THRD; ++i) { if(pthread_create(&amp;inc_x_thread[i], NULL, runner, NULL)) { fprintf(stderr, "Error creating thread\n"); return -1; } } for (int i = 0; i &lt; NUM_THRD; ++i) { if(pthread_join(inc_x_thread[i], NULL)) { fprintf(stderr, "Error joining thread\n"); return -1; } } return 0; } </pre> </description> <category>Ticket</category> </item> </channel> </rss>