PowerToys/src/common/interop/async_message_queue.h
Andrey Nekrasov 212ea2de30
common: refactor common library pt2 (#8588)
- remove common lib
- split settings, remove common-md
- move ipc interop/kb_layout to interop
- rename core -> settings, settings -> old_settings
- os-detect header-only; interop -> PowerToysInterop
- split notifications, move single-use headers where they're used
- winstore lib
- rename com utils
- rename Updating and Telemetry projects
- rename core -> settings-ui and remove examples folder
- rename settings-ui folder + consisent common/version include
2020-12-15 15:16:09 +03:00

55 lines
1.3 KiB
C++

#pragma once
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <string>
class AsyncMessageQueue
{
private:
std::mutex queue_mutex;
std::queue<std::wstring> message_queue;
std::condition_variable message_ready;
bool interrupted = false;
//Disable copy
AsyncMessageQueue(const AsyncMessageQueue&);
AsyncMessageQueue& operator=(const AsyncMessageQueue&);
public:
AsyncMessageQueue()
{
}
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();
}
};