Skip to content

Commit

Permalink
Added deque_views. Make use of sync_queue and refactor to use sync_qu…
Browse files Browse the repository at this point in the history
…eue removing _front and _back.
  • Loading branch information
viboes committed Nov 9, 2014
1 parent 3abdb86 commit 9b0705c
Show file tree
Hide file tree
Showing 16 changed files with 1,224 additions and 197 deletions.
24 changes: 12 additions & 12 deletions example/producer_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
typedef std::ostream the_ostream;
typedef std::istream the_istream;
#endif
#include <boost/thread/concurrent_queues/sync_deque.hpp>
#include <boost/thread/concurrent_queues/sync_queue.hpp>

void producer(the_ostream &mos, boost::sync_deque<int> & sbq)
void producer(the_ostream &mos, boost::sync_queue<int> & sbq)
{
using namespace boost;
try {
for(int i=0; ;++i)
{
sbq.push_back(i);
sbq.push(i);
//sbq << i;
mos << "push_back(" << i << ") "<< sbq.size()<<"\n";
mos << "push(" << i << ") "<< sbq.size()<<"\n";
this_thread::sleep_for(chrono::milliseconds(200));
}
}
Expand All @@ -48,14 +48,14 @@ void producer(the_ostream &mos, boost::sync_deque<int> & sbq)

void consumer(
the_ostream &mos,
boost::sync_deque<int> & sbq)
boost::sync_queue<int> & sbq)
{
using namespace boost;
try {
for(int i=0; ;++i)
{
int r;
sbq.pull_front(r);
sbq.pull(r);
//sbq >> r;
mos << i << " pull(" << r << ") "<< sbq.size()<<"\n";

Expand All @@ -71,14 +71,14 @@ void consumer(
mos << "exception !!!\n";
}
}
void consumer2(the_ostream &mos, boost::sync_deque<int> & sbq)
void consumer2(the_ostream &mos, boost::sync_queue<int> & sbq)
{
using namespace boost;
try {
for(int i=0; ;++i)
{
int r;
queue_op_status st = sbq.try_pull_front(r);
queue_op_status st = sbq.try_pull(r);
if (queue_op_status::closed == st) break;
if (queue_op_status::success == st) {
mos << i << " pull(" << r << ")\n";
Expand All @@ -91,16 +91,16 @@ void consumer2(the_ostream &mos, boost::sync_deque<int> & sbq)
mos << "exception !!!\n";
}
}
void consumer3(the_ostream &mos, boost::sync_deque<int> & sbq)
void consumer3(the_ostream &mos, boost::sync_queue<int> & sbq)
{
using namespace boost;
try {
for(int i=0; ;++i)
{
int r;
queue_op_status res = sbq.wait_pull_front(r);
queue_op_status res = sbq.wait_pull(r);
if (res==queue_op_status::closed) break;
mos << i << " wait_pull_front(" << r << ")\n";
mos << i << " wait_pull(" << r << ")\n";
this_thread::sleep_for(chrono::milliseconds(250));
}
}
Expand All @@ -126,7 +126,7 @@ int main()
//the_istream &mcin = std::cin;
#endif

sync_deque<int> sbq;
sync_queue<int> sbq;

{
mcout << "begin of main" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions example/producer_consumer2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
typedef std::ostream the_ostream;
typedef std::istream the_istream;
#endif
#include <boost/thread/concurrent_queues/sync_deque.hpp>
#include <boost/thread/concurrent_queues/sync_queue.hpp>
#include <boost/thread/concurrent_queues/queue_adaptor.hpp>
#include <boost/thread/concurrent_queues/queue_views.hpp>
#include <boost/static_assert.hpp>
Expand Down Expand Up @@ -129,7 +129,7 @@ int main()
//the_istream &mcin = std::cin;
#endif

queue_adaptor<sync_deque<int> > sbq;
queue_adaptor<sync_queue<int> > sbq;

{
mcout << "begin of main" << std::endl;
Expand Down
209 changes: 209 additions & 0 deletions include/boost/thread/concurrent_queues/deque_adaptor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#ifndef BOOST_THREAD_CONCURRENT_DEQUE_ADAPTOR_HPP
#define BOOST_THREAD_CONCURRENT_DEQUE_ADAPTOR_HPP

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Vicente J. Botet Escriba 2014. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/thread for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/thread/detail/config.hpp>
#include <boost/thread/detail/move.hpp>
#include <boost/thread/concurrent_queues/queue_op_status.hpp>
#include <boost/thread/concurrent_queues/deque_base.hpp>

#include <boost/config/abi_prefix.hpp>

namespace boost
{
namespace concurrent
{
namespace detail
{

template <typename Queue>
class deque_adaptor_copyable_only :
public boost::deque_base<typename Queue::value_type>
{
Queue queue;
public:
typedef typename Queue::value_type value_type;
typedef std::size_t size_type;

// Constructors/Assignment/Destructors
deque_adaptor_copyable_only() {}

// Observers
bool empty() const { return queue.empty(); }
bool full() const { return queue.full(); }
size_type size() const { return queue.size(); }
bool closed() const { return queue.closed(); }

// Modifiers
void close() { queue.close(); }

void push_back(const value_type& x) { queue.push_back(x); }

void pull_front(value_type& x) { queue.pull_front(x); };
value_type pull_front() { return queue.pull_front(); }

queue_op_status try_push_back(const value_type& x) { return queue.try_push_back(x); }
queue_op_status try_pull_front(value_type& x) { return queue.try_pull_front(x); }

queue_op_status nonblocking_push_back(const value_type& x) { return queue.nonblocking_push_back(x); }
queue_op_status nonblocking_pull_front(value_type& x) { return queue.nonblocking_pull_front(x); }

queue_op_status wait_push_back(const value_type& x) { return queue.wait_push_back(x); }
queue_op_status wait_pull_front(value_type& x) { return queue.wait_pull_front(x); }

};
template <typename Queue>
class deque_adaptor_movable_only :
public boost::deque_base<typename Queue::value_type>
{
Queue queue;
public:
typedef typename Queue::value_type value_type;
typedef std::size_t size_type;

// Constructors/Assignment/Destructors

deque_adaptor_movable_only() {}

// Observers
bool empty() const { return queue.empty(); }
bool full() const { return queue.full(); }
size_type size() const { return queue.size(); }
bool closed() const { return queue.closed(); }

// Modifiers
void close() { queue.close(); }


void pull_front(value_type& x) { queue.pull_front(x); };
// enable_if is_nothrow_copy_movable<value_type>
value_type pull_front() { return queue.pull_front(); }

queue_op_status try_pull_front(value_type& x) { return queue.try_pull_front(x); }

queue_op_status nonblocking_pull_front(value_type& x) { return queue.nonblocking_pull_front(x); }

queue_op_status wait_pull_front(value_type& x) { return queue.wait_pull_front(x); }

void push_back(BOOST_THREAD_RV_REF(value_type) x) { queue.push_back(boost::move(x)); }
queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x) { return queue.try_push_back(boost::move(x)); }
queue_op_status nonblocking_push_back(BOOST_THREAD_RV_REF(value_type) x) { return queue.nonblocking_push_back(boost::move(x)); }
queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x) { return queue.wait_push_back(boost::move(x)); }
};

template <typename Queue>
class deque_adaptor_copyable_and_movable :
public boost::deque_base<typename Queue::value_type>
{
Queue queue;
public:
typedef typename Queue::value_type value_type;
typedef std::size_t size_type;

// Constructors/Assignment/Destructors

deque_adaptor_copyable_and_movable() {}

// Observers
bool empty() const { return queue.empty(); }
bool full() const { return queue.full(); }
size_type size() const { return queue.size(); }
bool closed() const { return queue.closed(); }

// Modifiers
void close() { queue.close(); }


void push_back(const value_type& x) { queue.push_back(x); }

void pull_front(value_type& x) { queue.pull_front(x); };
// enable_if is_nothrow_copy_movable<value_type>
value_type pull_front() { return queue.pull_front(); }

queue_op_status try_push_back(const value_type& x) { return queue.try_push_back(x); }
queue_op_status try_pull_front(value_type& x) { return queue.try_pull_front(x); }

queue_op_status nonblocking_push_back(const value_type& x) { return queue.nonblocking_push_back(x); }
queue_op_status nonblocking_pull_front(value_type& x) { return queue.nonblocking_pull_front(x); }

queue_op_status wait_push_back(const value_type& x) { return queue.wait_push_back(x); }
queue_op_status wait_pull_front(value_type& x) { return queue.wait_pull_front(x); }

void push_back(BOOST_THREAD_RV_REF(value_type) x) { queue.push_back(boost::move(x)); }
queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x) { return queue.try_push_back(boost::move(x)); }
queue_op_status nonblocking_push_back(BOOST_THREAD_RV_REF(value_type) x) { return queue.nonblocking_push_back(boost::move(x)); }
queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x) { return queue.wait_push_back(boost::move(x)); }
};


template <class Q, class T,
#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
#if defined __GNUC__
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 5) || !defined(__GXX_EXPERIMENTAL_CXX0X__)
bool Copyable = is_copy_constructible<T>::value,
bool Movable = true
#else
bool Copyable = std::is_copy_constructible<T>::value && std::is_copy_assignable<T>::value,
bool Movable = std::is_move_constructible<T>::value && std::is_move_assignable<T>::value
#endif // __GNUC__
#elif defined _MSC_VER
#if _MSC_VER < 1700
bool Copyable = is_copy_constructible<T>::value,
bool Movable = true
#else
bool Copyable = std::is_copy_constructible<T>::value && std::is_copy_assignable<T>::value,
bool Movable = std::is_move_constructible<T>::value && std::is_move_assignable<T>::value
#endif // _MSC_VER
#else
bool Copyable = std::is_copy_constructible<T>::value && std::is_copy_assignable<T>::value,
bool Movable = std::is_move_constructible<T>::value && std::is_move_assignable<T>::value
#endif
#else
bool Copyable = is_copy_constructible<T>::value,
bool Movable = has_move_emulation_enabled<T>::value
#endif
>
struct deque_adaptor;

template <class Q, class T>
struct deque_adaptor<Q, T, true, true> {
typedef deque_adaptor_copyable_and_movable<Q> type;
};
template <class Q, class T>
struct deque_adaptor<Q, T, true, false> {
typedef deque_adaptor_copyable_only<Q> type;
};
template <class Q, class T>
struct deque_adaptor<Q, T, false, true> {
typedef deque_adaptor_movable_only<Q> type;
};

}

template <typename Queue>
class deque_adaptor :
public detail::deque_adaptor<Queue, typename Queue::value_type>::type
{
public:
typedef typename Queue::value_type value_type;
typedef std::size_t size_type;
// Constructors/Assignment/Destructors
virtual ~deque_adaptor() {};
};
}
using concurrent::deque_adaptor;

}

#include <boost/config/abi_suffix.hpp>

#endif
Loading

0 comments on commit 9b0705c

Please sign in to comment.