Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82169 - trunk/libs/lockfree/test
From: tim_at_[hidden]
Date: 2012-12-22 10:50:32


Author: timblechmann
Date: 2012-12-22 10:50:31 EST (Sat, 22 Dec 2012)
New Revision: 82169
URL: http://svn.boost.org/trac/boost/changeset/82169

Log:
lockfree: fix race condition in testsuite
Text files modified:
   trunk/libs/lockfree/test/spsc_queue_test.cpp | 4 +++-
   trunk/libs/lockfree/test/test_common.hpp | 37 +++++++++++++++++++++++++------------
   2 files changed, 28 insertions(+), 13 deletions(-)

Modified: trunk/libs/lockfree/test/spsc_queue_test.cpp
==============================================================================
--- trunk/libs/lockfree/test/spsc_queue_test.cpp (original)
+++ trunk/libs/lockfree/test/spsc_queue_test.cpp 2012-12-22 10:50:31 EST (Sat, 22 Dec 2012)
@@ -299,8 +299,10 @@
         for(;;) {
             bool success = get_element();
             if (!running && !success)
- return;
+ break;
         }
+
+ while ( get_element() );
     }
 
     void run(void)

Modified: trunk/libs/lockfree/test/test_common.hpp
==============================================================================
--- trunk/libs/lockfree/test/test_common.hpp (original)
+++ trunk/libs/lockfree/test/test_common.hpp 2012-12-22 10:50:31 EST (Sat, 22 Dec 2012)
@@ -63,22 +63,35 @@
     boost::lockfree::detail::atomic<bool> running;
 
     template <typename queue>
- void get_items(queue & stk)
+ bool consume_element(queue & q)
+ {
+ long id;
+ bool ret = q.pop(id);
+
+ if (!ret)
+ return false;
+
+ bool erased = data.erase(id);
+ bool inserted = dequeued.insert(id);
+ assert(erased);
+ assert(inserted);
+ ++pop_count;
+ return true;
+ }
+
+ template <typename queue>
+ void get_items(queue & q)
     {
         for (;;) {
- long id;
+ bool received_element = consume_element(q);
+ if (received_element)
+ continue;
 
- bool got = stk.pop(id);
- if (got) {
- bool erased = data.erase(id);
- bool inserted = dequeued.insert(id);
- assert(erased);
- assert(inserted);
- ++pop_count;
- } else
- if ( writers_finished.load() == writer_threads )
- return;
+ if ( writers_finished.load() == writer_threads )
+ break;
         }
+
+ while (consume_element(q));
     }
 
     template <typename queue>


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk