Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82104 - trunk/libs/lockfree/test
From: tim_at_[hidden]
Date: 2012-12-19 14:23:55


Author: timblechmann
Date: 2012-12-19 14:23:55 EST (Wed, 19 Dec 2012)
New Revision: 82104
URL: http://svn.boost.org/trac/boost/changeset/82104

Log:
lockfree: fix race condition in testsuite

thanks to peter dimov!
Text files modified:
   trunk/libs/lockfree/test/spsc_queue_test.cpp | 4 ++--
   trunk/libs/lockfree/test/test_common.hpp | 9 +++++----
   2 files changed, 7 insertions(+), 6 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-19 14:23:55 EST (Wed, 19 Dec 2012)
@@ -274,6 +274,7 @@
 
             ++spsc_queue_cnt;
         }
+ running = false;
     }
 
     bool get_element(void)
@@ -315,7 +316,6 @@
         writer.join();
         cout << "writer threads joined. waiting for readers to finish" << endl;
 
- running = false;
         reader.join();
 
         BOOST_REQUIRE_EQUAL(received_nodes, nodes_per_thread);
@@ -365,6 +365,7 @@
 
             spsc_queue_cnt+=buf_size;
         }
+ running = false;
     }
 
     bool get_elements(void)
@@ -409,7 +410,6 @@
         writer.join();
         cout << "writer threads joined. waiting for readers to finish" << endl;
 
- running = false;
         reader.join();
 
         BOOST_REQUIRE_EQUAL(received_nodes, nodes_per_thread);

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-19 14:23:55 EST (Wed, 19 Dec 2012)
@@ -31,6 +31,8 @@
     const int reader_threads;
     const int writer_threads;
 
+ atomic_int writers_finished;
+
     static_hashed_set<long, buckets> data;
     static_hashed_set<long, buckets> dequeued;
     array<std::set<long>, buckets> returned;
@@ -58,6 +60,7 @@
                     /*thread::yield()*/;
             ++push_count;
         }
+ writers_finished += 1;
     }
 
     boost::lockfree::detail::atomic<bool> running;
@@ -76,7 +79,7 @@
                 assert(inserted);
                 ++pop_count;
             } else
- if (!running.load())
+ if ( writers_finished.load() == writer_threads )
                     return;
         }
     }
@@ -85,8 +88,7 @@
     void run(queue & stk)
     {
         BOOST_WARN(stk.is_lock_free());
-
- running.store(true);
+ writers_finished.store(0);
 
         thread_group writer;
         thread_group reader;
@@ -106,7 +108,6 @@
 
         cout << "writer threads joined, waiting for readers" << endl;
 
- running = false;
         reader.join_all();
 
         cout << "reader threads joined" << endl;


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