69 lines
1.8 KiB
C++
69 lines
1.8 KiB
C++
#pragma once
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
/**
|
|
* @file
|
|
* @copyright MPLv2
|
|
*/
|
|
#include <mutex>
|
|
#include <queue>
|
|
#include <memory>
|
|
#include <condition_variable>
|
|
#include <string>
|
|
|
|
namespace WHISPER{
|
|
/**
|
|
* @class BasicMessageQueue
|
|
*
|
|
* This class encapsulates shared storage which is a queue and protect it
|
|
* with a mutex. In Addition there is a condition variable to notify other
|
|
* threads if there is new data in the queue.
|
|
*/
|
|
template<class T>
|
|
class threadSafeQueue
|
|
{
|
|
private:
|
|
std::queue<std::unique_ptr<T>> q;
|
|
std::mutex mx;
|
|
std::condition_variable condVar;
|
|
public:
|
|
/**
|
|
* @brief default constructor for class BasicMessageQueue
|
|
*/
|
|
threadSafeQueue();
|
|
|
|
/**
|
|
* @brief appends given message to queue
|
|
*
|
|
* The storage is protected by a mutex. Makes a notify_one call to inform
|
|
* in waiting thread that there is a new message in the queue.
|
|
*
|
|
* @param msg - incoming message
|
|
*/
|
|
void addElement( std::unique_ptr<T> msg );
|
|
|
|
/**
|
|
* @brief gets the fron message from the queue
|
|
*
|
|
* This method gets the front message in the queue and deletes it from the
|
|
* queue. The storage is protected by a mutex. If the queue is empty the
|
|
* function throws an exception.
|
|
*
|
|
* @result msg - returns the front message from the queue
|
|
* @throws length_error - if queue is empty this method throws length_error exception
|
|
*/
|
|
std::unique_ptr<T> getElement();
|
|
|
|
/**
|
|
* @brief method size
|
|
*
|
|
* @result size - current size of the message queue
|
|
*/
|
|
unsigned int size();
|
|
|
|
}; //BasicMessageQueue
|
|
|
|
}; // namespace BC
|