LunaHook-mirror/LunaHost/GUI/lockedqueue.hpp

36 lines
578 B
C++
Raw Normal View History

2024-07-21 19:28:47 +08:00
template <class T>
class lockedqueue
{
std::mutex lock;
2024-07-21 19:28:47 +08:00
std::queue<T> data;
HANDLE hsema;
2024-07-21 19:28:47 +08:00
public:
lockedqueue()
{
hsema = CreateSemaphore(NULL, 0, 65535, NULL);
}
2024-07-21 19:28:47 +08:00
~lockedqueue()
{
CloseHandle(hsema);
}
2024-07-21 19:28:47 +08:00
void push(T _)
{
std::lock_guard _l(lock);
data.push(std::move(_));
2024-07-21 19:28:47 +08:00
ReleaseSemaphore(hsema, 1, NULL);
}
2024-07-21 19:28:47 +08:00
T pop()
{
WaitForSingleObject(hsema, INFINITE);
std::lock_guard _l(lock);
2024-07-21 19:28:47 +08:00
auto _ = data.front();
data.pop();
return _;
}
2024-07-21 19:28:47 +08:00
bool empty()
{
std::lock_guard _l(lock);
return data.empty();
}
};