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
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2011-10-01 18:45:20 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2011-10-01 18:45:20 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-08 09:10:46 +00:00
|
|
|
#ifndef GNSS_SDR_CONCURRENT_QUEUE_H
|
|
|
|
#define GNSS_SDR_CONCURRENT_QUEUE_H
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2019-07-16 09:07:02 +00:00
|
|
|
#include <chrono>
|
2019-07-16 10:02:13 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2018-02-26 02:15:53 +00:00
|
|
|
#include <queue>
|
2019-07-16 10:02:13 +00:00
|
|
|
#include <thread>
|
2013-07-15 17:07:10 +00:00
|
|
|
|
2020-11-01 12:37:19 +00:00
|
|
|
/** \addtogroup Core
|
|
|
|
* \{ */
|
|
|
|
/** \addtogroup Core_Receiver
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename Data>
|
2011-10-01 18:45:20 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \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
|
2020-02-05 20:24:46 +00:00
|
|
|
* https://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
|
2011-10-01 18:45:20 +00:00
|
|
|
*/
|
2019-02-22 09:47:24 +00:00
|
|
|
class Concurrent_Queue
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void push(Data const& data)
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock(the_mutex);
|
2011-10-01 18:45:20 +00:00
|
|
|
the_queue.push(data);
|
|
|
|
lock.unlock();
|
|
|
|
the_condition_variable.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock(the_mutex);
|
2011-10-01 18:45:20 +00:00
|
|
|
return the_queue.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool try_pop(Data& popped_value)
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock(the_mutex);
|
2018-03-03 01:03:39 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock(the_mutex);
|
2018-03-03 01:03:39 +00:00
|
|
|
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();
|
|
|
|
}
|
2019-07-16 10:02:13 +00:00
|
|
|
|
2019-07-16 09:07:02 +00:00
|
|
|
bool timed_wait_and_pop(Data& popped_value, int wait_ms)
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock(the_mutex);
|
2019-07-16 09:07:02 +00:00
|
|
|
if (the_queue.empty())
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
the_condition_variable.wait_for(lock, std::chrono::milliseconds(wait_ms));
|
2019-07-16 09:07:02 +00:00
|
|
|
if (the_queue.empty())
|
|
|
|
{
|
2019-07-16 10:02:13 +00:00
|
|
|
return false;
|
2019-07-16 09:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
popped_value = the_queue.front();
|
|
|
|
the_queue.pop();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:44:42 +00:00
|
|
|
private:
|
|
|
|
std::queue<Data> the_queue;
|
2019-07-16 09:07:02 +00:00
|
|
|
mutable std::mutex the_mutex;
|
|
|
|
std::condition_variable the_condition_variable;
|
2011-10-01 18:45:20 +00:00
|
|
|
};
|
2019-09-13 06:56:37 +00:00
|
|
|
|
2020-11-01 12:37:19 +00:00
|
|
|
|
|
|
|
/** \} */
|
|
|
|
/** \} */
|
2020-02-08 09:10:46 +00:00
|
|
|
#endif // GNSS_SDR_CONCURRENT_QUEUE_H
|