2011-10-01 18:45:20 +00:00
|
|
|
/*!
|
|
|
|
* \file concurrent_queue.h
|
2011-12-28 21:36:45 +00:00
|
|
|
* \brief Interface of a thread-safe std::queue
|
2011-10-01 18:45:20 +00:00
|
|
|
* \author Javier Arribas, 2011. jarribas(at)cttc.es
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
2015-01-12 19:31:04 +00:00
|
|
|
* Copyright (C) 2010-2015 (see AUTHORS file for a list of contributors)
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
|
|
|
* GNSS-SDR is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2015-01-08 18:49:59 +00:00
|
|
|
* (at your option) any later version.
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNSS-SDR. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GNSS_SDR_CONCURRENT_QUEUE_H
|
|
|
|
#define GNSS_SDR_CONCURRENT_QUEUE_H
|
|
|
|
|
2013-07-15 17:07:10 +00:00
|
|
|
#include <queue>
|
2015-01-09 12:47:52 +00:00
|
|
|
#include <boost/thread.hpp>
|
2013-07-15 17:07:10 +00:00
|
|
|
|
2011-10-01 18:45:20 +00:00
|
|
|
template<typename Data>
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief This class implements a thread-safe std::queue
|
|
|
|
*
|
|
|
|
* Thread-safe object queue which uses the library
|
|
|
|
* boost_thread to perform MUTEX based on the code available at
|
|
|
|
* http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
|
|
|
|
*/
|
|
|
|
class concurrent_queue
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::queue<Data> the_queue;
|
|
|
|
mutable boost::mutex the_mutex;
|
|
|
|
boost::condition_variable the_condition_variable;
|
|
|
|
public:
|
|
|
|
void push(Data const& data)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
|
|
|
the_queue.push(data);
|
|
|
|
lock.unlock();
|
|
|
|
the_condition_variable.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
|
|
|
return the_queue.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool try_pop(Data& popped_value)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
|
|
|
if(the_queue.empty())
|
2011-12-28 21:36:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-01 17:40:26 +00:00
|
|
|
popped_value = the_queue.front();
|
2011-10-01 18:45:20 +00:00
|
|
|
the_queue.pop();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_and_pop(Data& popped_value)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
|
|
|
while(the_queue.empty())
|
2011-12-28 21:36:45 +00:00
|
|
|
{
|
|
|
|
the_condition_variable.wait(lock);
|
|
|
|
}
|
2012-11-01 17:40:26 +00:00
|
|
|
popped_value = the_queue.front();
|
2011-10-01 18:45:20 +00:00
|
|
|
the_queue.pop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|