concurrentQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <iostream>
19 #include <boost/thread.hpp>
20 #include <boost/circular_buffer.hpp>
21 
22 template<typename T>
24 {
25 private:
26  boost::circular_buffer<T> _queue;
27  mutable boost::mutex _mutex;
28  boost::condition_variable _condition;
29 public:
31  : _queue(2)
32  {
33  }
34 
35  bool empty() const
36  {
37  boost::mutex::scoped_lock lock(_mutex);
38  return _queue.empty();
39  }
40 
41  void push(T const& data)
42  {
43  boost::mutex::scoped_lock lock(_mutex);
44  _queue.push_back(data);
45  lock.unlock();
46  _condition.notify_one();
47  }
48 
49  bool pop(T& data)
50  {
51  boost::mutex::scoped_lock lock(_mutex);
52  if(_queue.empty())
53  return false;
54 
55  data = _queue.front();
56  _queue.pop_front();
57 
58  return true;
59  }
60 
61  void wait_pop(T& data)
62  {
63  boost::mutex::scoped_lock lock(_mutex);
64  while(_queue.empty())
65  _condition.wait(lock);
66 
67  data=_queue.front();
68  _queue.pop_front();
69  }
70 };
boost::mutex _mutex
void wait_pop(T &data)
bool empty() const
void push(T const &data)
boost::condition_variable _condition
boost::circular_buffer< T > _queue
bool pop(T &data)


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39