-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalQueue.h
More file actions
63 lines (54 loc) · 1.44 KB
/
Copy pathLocalQueue.h
File metadata and controls
63 lines (54 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** ======================================================================+
+ Copyright @2015-2021 Arjun Ray
+ Released under MIT License: see https://mit-license.org
+========================================================================*/
#pragma once
#ifndef UTILITY_LOCALQUEUE_H
#define UTILITY_LOCALQUEUE_H
#include "BasicQueue.h"
#include <thread>
#include <utility>
namespace Utility
{
/**
* @class LocalQueue
* @brief Multi-producer, single-consumer queue.
*/
template<typename Handler>
class LocalQueue
{
using Item = typename Handler::item_type;
using Queue = BasicQueue<Item>;
using Worker = std::thread;
public:
~LocalQueue() noexcept { stop_(); }
//
template<typename... Args>
LocalQueue(Args&&... args)
: handler_(std::forward<Args>(args)...)
, worker_([this]() -> void { queue_.pump( handler_ ); })
{}
bool put( Item& item )
{
return queue_.put( std::move(item) );
}
bool put( Item&& item )
{
return queue_.put( std::move(item) );
}
//void stop()
//{
// queue_.stop();
//}
private:
Queue queue_;
Handler handler_;
Worker worker_;
void stop_()
{
queue_.stop();
worker_.join();
}
};
} // namespace Utility
#endif // UTILITY_LOCALQUEUE_H