2019-09-05 00:26:26 +08:00
|
|
|
#pragma once
|
|
|
|
#include <queue>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <string>
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
class AsyncMessageQueue
|
|
|
|
{
|
2019-09-05 00:26:26 +08:00
|
|
|
private:
|
2020-03-05 18:07:06 +08:00
|
|
|
std::mutex queue_mutex;
|
|
|
|
std::queue<std::wstring> message_queue;
|
|
|
|
std::condition_variable message_ready;
|
|
|
|
bool interrupted = false;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
//Disable copy
|
|
|
|
AsyncMessageQueue(const AsyncMessageQueue&);
|
|
|
|
AsyncMessageQueue& operator=(const AsyncMessageQueue&);
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
public:
|
2020-03-05 18:07:06 +08:00
|
|
|
AsyncMessageQueue()
|
|
|
|
{
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-03-05 18:07:06 +08:00
|
|
|
void queue_message(std::wstring message)
|
|
|
|
{
|
|
|
|
this->queue_mutex.lock();
|
|
|
|
this->message_queue.push(message);
|
|
|
|
this->queue_mutex.unlock();
|
|
|
|
this->message_ready.notify_one();
|
|
|
|
}
|
|
|
|
std::wstring pop_message()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(this->queue_mutex);
|
|
|
|
while (message_queue.empty() && !this->interrupted)
|
|
|
|
{
|
|
|
|
this->message_ready.wait(lock);
|
|
|
|
}
|
|
|
|
if (this->interrupted)
|
|
|
|
{
|
|
|
|
//Just returns a empty string if the queue was interrupted.
|
|
|
|
return std::wstring(L"");
|
|
|
|
}
|
|
|
|
std::wstring message = this->message_queue.front();
|
|
|
|
this->message_queue.pop();
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
void interrupt()
|
|
|
|
{
|
|
|
|
this->queue_mutex.lock();
|
|
|
|
this->interrupted = true;
|
|
|
|
this->queue_mutex.unlock();
|
|
|
|
this->message_ready.notify_all();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
};
|