concurrentQueue.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <iostream>
00019 #include <boost/thread.hpp>
00020 #include <boost/circular_buffer.hpp>
00021 
00022 template<typename T>
00023 class ConcurrentQueue
00024 {
00025 private:
00026     boost::circular_buffer<T> _queue;
00027     mutable boost::mutex _mutex;
00028     boost::condition_variable _condition;
00029 public:
00030     ConcurrentQueue()
00031         : _queue(2)
00032         {
00033         }
00034 
00035     bool empty() const
00036     {
00037         boost::mutex::scoped_lock lock(_mutex);
00038         return _queue.empty();
00039     }
00040 
00041     void push(T const& data)
00042     {
00043         boost::mutex::scoped_lock lock(_mutex);
00044         _queue.push_back(data);
00045         lock.unlock();
00046         _condition.notify_one();
00047     }
00048 
00049     bool pop(T& data)
00050     {
00051         boost::mutex::scoped_lock lock(_mutex);
00052         if(_queue.empty())
00053             return false;
00054 
00055         data = _queue.front();
00056         _queue.pop_front();
00057 
00058         return true;
00059     }
00060 
00061     void wait_pop(T& data)
00062     {
00063         boost::mutex::scoped_lock lock(_mutex);
00064         while(_queue.empty())
00065                 _condition.wait(lock);
00066 
00067         data=_queue.front();
00068         _queue.pop_front();
00069     }
00070 };


cob_light
Author(s): Benjamin Maidel
autogenerated on Sat Jun 8 2019 21:02:07