BlockingQueue.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * * Neither the name of Willow Garage, Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 /*
33  * BlockingQueue.hpp
34  *
35  * @date 13.11.2015
36  * @author Tristan Igelbrink (Tristan@Igelbrink.com)
37  */
38 
39 #ifndef BLOCKINGQUEUE_HPP__
40 #define BLOCKINGQUEUE_HPP__
41 
42 #include <deque>
43 #include <limits>
44 #include <boost/thread/mutex.hpp>
45 #include <boost/thread/condition.hpp>
46 #include <boost/cstdint.hpp>
47 #include <boost/any.hpp>
48 
49 // Blocking Queue based on the Java's implementation.
50 // This class is thread safe.
52 {
53 public:
54  enum { NoMaxSizeRestriction = 0 };
55  // default constructor
56  // provides no size restriction on the blocking queue
59  {
60  }
61 
62  // Retrieve and remove the oldest element in the queue.
63  // If there is no element available, this method will block until
64  // elements are inserted.
65  //
66  // This method is thread safe.
67  boost::any Take()
68  {
69  boost::mutex::scoped_lock guard(m_mutex);
70 
71  if(m_deque.empty() == true)
72  {
73  m_condSpaceAvailable.wait(guard);
74  }
75 
76  boost::any o = m_deque.back();
77 
78  m_deque.pop_back();
79 
80  return o;
81  }
82  // Insert a new element to the blocking queue.
83  // Return true if the operation is successful, false otherwise.
84  //
85  // This method is thread safe.
86  bool Add(boost::any const & o)
87  {
88  boost::mutex::scoped_lock guard(m_mutex);
89 
90  if( (m_deque.size() >= m_maxSize) &&
92  {
93  return false;
94  }
95 
96  m_deque.push_front(o);
97 
98  m_condSpaceAvailable.notify_one();
99 
100  return true;
101  }
102 
103  // return the maximum allowed size for this queue.
104  boost::uint64_t GetMaxSize() const { return m_maxSize; }
105 
106  // set the maximum allowed size for this queue.
107  void SetMaxSize(boost::uint64_t val) { m_maxSize = val; }
108 
109 private:
110 
111  std::deque<boost::any> m_deque;
112  boost::mutex m_mutex;
113  boost::uint64_t m_maxSize;
114  boost::condition m_condSpaceAvailable;
115 };
116 #endif // BLOCKINGQUEUE_HPP__
BlockingQueue::m_condSpaceAvailable
boost::condition m_condSpaceAvailable
Definition: BlockingQueue.hpp:114
BlockingQueue::m_maxSize
boost::uint64_t m_maxSize
Definition: BlockingQueue.hpp:113
BlockingQueue::Add
bool Add(boost::any const &o)
Definition: BlockingQueue.hpp:86
BlockingQueue::Take
boost::any Take()
Definition: BlockingQueue.hpp:67
BlockingQueue
Definition: BlockingQueue.hpp:51
BlockingQueue::SetMaxSize
void SetMaxSize(boost::uint64_t val)
Definition: BlockingQueue.hpp:107
BlockingQueue::GetMaxSize
boost::uint64_t GetMaxSize() const
Definition: BlockingQueue.hpp:104
BlockingQueue::m_mutex
boost::mutex m_mutex
Definition: BlockingQueue.hpp:112
BlockingQueue::m_deque
std::deque< boost::any > m_deque
Definition: BlockingQueue.hpp:111
BlockingQueue::BlockingQueue
BlockingQueue()
Definition: BlockingQueue.hpp:57
BlockingQueue::NoMaxSizeRestriction
@ NoMaxSizeRestriction
Definition: BlockingQueue.hpp:54


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:22