source: trunk/yat/utility/Queue.h @ 3751

Last change on this file since 3751 was 3751, checked in by Peter, 5 years ago

adjust license text to be same as in other files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#ifndef theplu_yat_utility_queue
2#define theplu_yat_utility_queue
3
4// $Id: Queue.h 3751 2018-10-17 01:49:41Z peter $
5//
6// Copyright (C) 2013, 2014, 2016, 2017 Peter Johansson
7//
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12//
13// This program is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16// General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with yat. If not, see <http://www.gnu.org/licenses/>.
20
21#include "config_public.h"
22
23#include "BasicQueue.h"
24#include "utility.h"
25#include "yat_assert.h"
26
27#include <boost/thread.hpp>
28
29#include <deque>
30#include <utility>
31
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  /**
37     \brief Multi-thread safe queue
38
39     This class provides a multi-thread safe queue. The Queue is
40     typically shared by multiple threads such that some threads push
41     elements and some pop elements. The Queue is a "first in first
42     out" container and holds the same functionality as the similar
43     <a href="http://www.sgi.com/tech/stl/queue.html">std::queue</a>. The
44     difference is that Queue is multi-thread safe, in other words,
45     when one thread access the Queue, other threads are locked out
46     from access so that only one thread touches the Queue at a time
47     and its behaviour is well defined. In a single-thread application
48     there is no point in using the class as std::queue should be
49     prefereble.
50
51     \note Copy constructor and assignment are available but they are
52     not thread safe in the current implementation.
53
54     \since New in yat 0.11
55
56     \see Boost Library provides a
57     <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/lockfree.html">
58     lock-free queue</a>
59   */
60  template<typename T>
61  class Queue :
62    public detail::BasicQueue<Queue<T>, T, std::deque<T> >
63  {
64    friend class detail::BasicQueue<Queue<T>, T, std::deque<T> >;
65    typedef detail::BasicQueue<Queue<T>, T, std::deque<T> > Base;
66  public:
67    /**
68       \brief Create a Queue with no elements
69    */
70    Queue(void) {}
71
72    /**
73       \brief Copy constructor
74    */
75    Queue(const Queue& other)
76      : Base(other) {}
77
78    /**
79       \brief assignment operator
80     */
81    Queue& operator=(const Queue& lhs)
82    {
83      this->assign(lhs);
84      return *this;
85    }
86
87  private:
88    void pop_impl(T& value, boost::unique_lock<boost::mutex>& lock)
89    {
90      YAT_ASSERT(this->q_.size());
91      value = YAT_MOVE_IF_NOEXCEPT(this->q_.front());
92      this->q_.pop_front();
93    }
94
95
96    void push_impl(const T& value, boost::unique_lock<boost::mutex>& lock)
97    {
98      this->q_.push_back(value);
99    }
100
101
102#ifdef YAT_HAVE_RVALUE
103    void push_impl(T&& value, boost::unique_lock<boost::mutex>& lock)
104    {
105      this->q_.push_back(std::move(value));
106    }
107#endif
108  };
109
110}}}
111#endif
Note: See TracBrowser for help on using the repository browser.