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