Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85861 - in trunk: boost/thread libs/thread/test/sync/mutual_exclusion/sync_bounded_queue libs/thread/test/sync/mutual_exclusion/sync_queue
From: vicente.botet_at_[hidden]
Date: 2013-09-23 17:40:08


Author: viboes
Date: 2013-09-23 17:40:08 EDT (Mon, 23 Sep 2013)
New Revision: 85861
URL: http://svn.boost.org/trac/boost/changeset/85861

Log:
Thread: make it possible to use non default constructible types in sync_queue, but sync_bounded_queue requires it yet.

Text files modified:
   trunk/boost/thread/sync_bounded_queue.hpp | 13 +++++++--
   trunk/boost/thread/sync_queue.hpp | 12 ++++++--
   trunk/libs/thread/test/sync/mutual_exclusion/sync_bounded_queue/single_thread_pass.cpp | 32 ++++++++++++++++--------
   trunk/libs/thread/test/sync/mutual_exclusion/sync_queue/single_thread_pass.cpp | 52 +++++++++++++++++++++++----------------
   4 files changed, 71 insertions(+), 38 deletions(-)

Modified: trunk/boost/thread/sync_bounded_queue.hpp
==============================================================================
--- trunk/boost/thread/sync_bounded_queue.hpp Mon Sep 23 16:30:48 2013 (r85860)
+++ trunk/boost/thread/sync_bounded_queue.hpp 2013-09-23 17:40:08 EDT (Mon, 23 Sep 2013) (r85861)
@@ -157,6 +157,13 @@
       out_ = inc(out_);
       notify_not_full_if_needed(lk);
     }
+ inline value_type pull(unique_lock<mutex>& lk)
+ {
+ value_type elem = boost::move(data_[out_]);
+ out_ = inc(out_);
+ notify_not_full_if_needed(lk);
+ return boost::move(elem);
+ }
     inline boost::shared_ptr<value_type> ptr_pull(unique_lock<mutex>& lk)
     {
       shared_ptr<value_type> res = make_shared<value_type>(boost::move(data_[out_]));
@@ -410,9 +417,9 @@
   {
     try
     {
- value_type elem;
- pull(elem);
- return boost::move(elem);
+ unique_lock<mutex> lk(mtx_);
+ wait_until_not_empty(lk);
+ return pull(lk);
     }
     catch (...)
     {

Modified: trunk/boost/thread/sync_queue.hpp
==============================================================================
--- trunk/boost/thread/sync_queue.hpp Mon Sep 23 16:30:48 2013 (r85860)
+++ trunk/boost/thread/sync_queue.hpp 2013-09-23 17:40:08 EDT (Mon, 23 Sep 2013) (r85861)
@@ -115,6 +115,12 @@
       elem = boost::move(data_.front());
       data_.pop_front();
     }
+ inline value_type pull(unique_lock<mutex>& )
+ {
+ value_type e = boost::move(data_.front());
+ data_.pop_front();
+ return boost::move(e);
+ }
     inline boost::shared_ptr<value_type> ptr_pull(unique_lock<mutex>& )
     {
       shared_ptr<value_type> res = make_shared<value_type>(boost::move(data_.front()));
@@ -347,9 +353,9 @@
   {
     try
     {
- value_type elem;
- pull(elem);
- return boost::move(elem);
+ unique_lock<mutex> lk(mtx_);
+ wait_until_not_empty(lk);
+ return pull(lk);
     }
     catch (...)
     {

Modified: trunk/libs/thread/test/sync/mutual_exclusion/sync_bounded_queue/single_thread_pass.cpp
==============================================================================
--- trunk/libs/thread/test/sync/mutual_exclusion/sync_bounded_queue/single_thread_pass.cpp Mon Sep 23 16:30:48 2013 (r85860)
+++ trunk/libs/thread/test/sync/mutual_exclusion/sync_bounded_queue/single_thread_pass.cpp 2013-09-23 17:40:08 EDT (Mon, 23 Sep 2013) (r85861)
@@ -17,14 +17,24 @@
 
 class non_copyable
 {
- BOOST_THREAD_MOVABLE_ONLY(non_copyable)
+ BOOST_THREAD_MOVABLE_ONLY(non_copyable)
+ int val;
+public:
+ non_copyable() {}
+ non_copyable(int v) : val(v){}
+ non_copyable(BOOST_RV_REF(non_copyable) x): val(x.val) {}
+ non_copyable& operator=(BOOST_RV_REF(non_copyable) x) { val=x.val; return *this; }
+ bool operator==(non_copyable const& x) const {return val==x.val;}
+ template <typename OSTREAM>
+ friend OSTREAM& operator <<(OSTREAM& os, non_copyable const&x )
+ {
+ os << x.val;
+ return os;
+ }
 
- public:
- non_copyable(){}
- non_copyable(BOOST_RV_REF(non_copyable)) {}
- non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; }
 };
 
+
 int main()
 {
 
@@ -68,7 +78,7 @@
   {
     // empty queue push rvalue succeeds
       boost::sync_bounded_queue<non_copyable> q(2);
- non_copyable nc;
+ non_copyable nc(1);
       q.push(boost::move(nc));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
@@ -107,7 +117,7 @@
   {
     // empty queue try_push rvalue succeeds
       boost::sync_bounded_queue<non_copyable> q(2);
- non_copyable nc;
+ non_copyable nc(1);
       BOOST_TEST(q.try_push(boost::move(nc)));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
@@ -136,7 +146,7 @@
   {
     // empty queue try_push rvalue succeeds
       boost::sync_bounded_queue<non_copyable> q(2);
- non_copyable nc;
+ non_copyable nc(1);
       BOOST_TEST(q.try_push(boost::no_block, boost::move(nc)));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
@@ -158,11 +168,11 @@
   {
     // 1-element queue pull succeed
       boost::sync_bounded_queue<non_copyable> q(2);
- non_copyable nc;
+ non_copyable nc(1);
       q.push(boost::move(nc));
- non_copyable nc2;
+ non_copyable nc2(2);
       q.pull(nc2);
- //BOOST_TEST_EQ(nc, nc2, 1);
+ BOOST_TEST_EQ(nc, nc2);
       BOOST_TEST(q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 0u);

Modified: trunk/libs/thread/test/sync/mutual_exclusion/sync_queue/single_thread_pass.cpp
==============================================================================
--- trunk/libs/thread/test/sync/mutual_exclusion/sync_queue/single_thread_pass.cpp Mon Sep 23 16:30:48 2013 (r85860)
+++ trunk/libs/thread/test/sync/mutual_exclusion/sync_queue/single_thread_pass.cpp 2013-09-23 17:40:08 EDT (Mon, 23 Sep 2013) (r85861)
@@ -17,14 +17,24 @@
 
 class non_copyable
 {
- BOOST_THREAD_MOVABLE_ONLY(non_copyable)
+ BOOST_THREAD_MOVABLE_ONLY(non_copyable)
+ int val;
+public:
+ non_copyable(int v) : val(v){}
+ non_copyable(BOOST_RV_REF(non_copyable) x): val(x.val) {}
+ non_copyable& operator=(BOOST_RV_REF(non_copyable) x) { val=x.val; return *this; }
+ bool operator==(non_copyable const& x) const {return val==x.val;}
+ template <typename OSTREAM>
+ friend OSTREAM& operator <<(OSTREAM& os, non_copyable const&x )
+ {
+ os << x.val;
+ return os;
+ }
 
- public:
- non_copyable(){}
- non_copyable(BOOST_RV_REF(non_copyable)) {}
- non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; }
 };
 
+
+
 int main()
 {
 
@@ -78,7 +88,7 @@
   {
     // empty queue push rvalue/non_copyable succeeds
       boost::sync_queue<non_copyable> q;
- q.push(non_copyable());
+ q.push(non_copyable(1));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 1u);
@@ -88,7 +98,7 @@
   {
     // empty queue push rvalue/non_copyable succeeds
       boost::sync_queue<non_copyable> q;
- non_copyable nc;
+ non_copyable nc(1);
       q.push(boost::move(nc));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
@@ -148,7 +158,7 @@
   {
     // empty queue try_push rvalue/non-copyable succeeds
       boost::sync_queue<non_copyable> q;
- non_copyable nc;
+ non_copyable nc(1);
       BOOST_TEST(q.try_push(boost::move(nc)));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
@@ -179,7 +189,7 @@
   {
     // empty queue try_push rvalue/non-copyable succeeds
       boost::sync_queue<non_copyable> q;
- BOOST_TEST(q.try_push(boost::no_block, non_copyable()));
+ BOOST_TEST(q.try_push(boost::no_block, non_copyable(1)));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 1u);
@@ -189,7 +199,7 @@
   {
     // empty queue try_push rvalue/non-copyable succeeds
       boost::sync_queue<non_copyable> q;
- non_copyable nc;
+ non_copyable nc(1);
       BOOST_TEST(q.try_push(boost::no_block, boost::move(nc)));
       BOOST_TEST(! q.empty());
       BOOST_TEST(! q.full());
@@ -211,11 +221,11 @@
   {
     // 1-element queue pull succeed
       boost::sync_queue<non_copyable> q;
- non_copyable nc1;
+ non_copyable nc1(1);
       q.push(boost::move(nc1));
- non_copyable nc2;
+ non_copyable nc2(2);
       q.pull(nc2);
- //BOOST_TEST_EQ(nc1, nc2);
+ BOOST_TEST_EQ(nc1, nc2);
       BOOST_TEST(q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 0u);
@@ -235,10 +245,10 @@
   {
     // 1-element queue pull succeed
       boost::sync_queue<non_copyable> q;
- non_copyable nc1;
+ non_copyable nc1(1);
       q.push(boost::move(nc1));
       non_copyable nc = q.pull();
- //BOOST_TEST_EQ(nc, 1);
+ BOOST_TEST_EQ(nc, nc1);
       BOOST_TEST(q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 0u);
@@ -259,11 +269,11 @@
   {
     // 1-element queue try_pull succeed
       boost::sync_queue<non_copyable> q;
- non_copyable nc1;
+ non_copyable nc1(1);
       q.push(boost::move(nc1));
- non_copyable nc;
+ non_copyable nc(2);
       BOOST_TEST(q.try_pull(nc));
- //BOOST_TEST_EQ(nc, 1);
+ BOOST_TEST_EQ(nc, nc1);
       BOOST_TEST(q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 0u);
@@ -284,11 +294,11 @@
   {
     // 1-element queue try_pull succeed
       boost::sync_queue<non_copyable> q;
- non_copyable nc1;
+ non_copyable nc1(1);
       q.push(boost::move(nc1));
- non_copyable nc;
+ non_copyable nc(2);
       BOOST_TEST(q.try_pull(boost::no_block, nc));
- //BOOST_TEST_EQ(nc, 1);
+ BOOST_TEST_EQ(nc, nc1);
       BOOST_TEST(q.empty());
       BOOST_TEST(! q.full());
       BOOST_TEST_EQ(q.size(), 0u);


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