ADD: Message container and Join Message
This commit is contained in:
60
src/WHISPER/threadSafeQueue.cpp
Normal file
60
src/WHISPER/threadSafeQueue.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 BasicMessageQueue.cpp
|
||||
* @brief class which encapsulates queue
|
||||
* @author Christina Sander <christina.sander@hsu-hh.de>
|
||||
* @date 25.06.2020
|
||||
* @copyright MPLv2
|
||||
*/
|
||||
#include <WHISPER/threadSafeQueue.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
template<class T>
|
||||
WHISPER::threadSafeQueue<T> threadSafeQueue(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// appends the given message to the message queue
|
||||
template<class T>
|
||||
void WHISPER::threadSafeQueue< T>::addElement(std::unique_ptr<T> elem)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mx);
|
||||
q.push( std::move(elem) );
|
||||
lk.unlock();
|
||||
condVar.notify_one();
|
||||
}
|
||||
|
||||
// gets a message from the queue
|
||||
template<class T>
|
||||
std::unique_ptr<T> WHISPER::threadSafeQueue<T>::getElement()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mx);
|
||||
if( 0 == q.size() )
|
||||
{
|
||||
lk.unlock();
|
||||
throw std::length_error("Empty Queue\n");
|
||||
}
|
||||
std::unique_ptr<std::string> elem = std::move( q.front() );
|
||||
q.pop();
|
||||
lk.unlock();
|
||||
return std::move(elem);
|
||||
}
|
||||
|
||||
|
||||
//returns the size of the message queue
|
||||
template<class T>
|
||||
unsigned int WHISPER::threadSafeQueue<T>::size()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mx);
|
||||
unsigned int size = q.size();
|
||||
lk.unlock();
|
||||
return size;
|
||||
}
|
||||
Reference in New Issue
Block a user