|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 #include "wait_queue.h"
|
|
|
4 #include <functional>
|
|
|
5 #include <mutex>
|
|
|
6 #include <memory>
|
|
|
7
|
|
|
8 namespace pfc {
|
|
|
9
|
|
|
10 class cmdThread {
|
|
|
11 public:
|
|
|
12 typedef std::function<void()> cmd_t;
|
|
|
13 typedef pfc::waitQueue<cmd_t> queue_t;
|
|
|
14
|
|
|
15 void add(cmd_t c) {
|
|
|
16 std::call_once(m_once, [this] {
|
|
|
17 auto q = std::make_shared<queue_t>();
|
|
|
18 pfc::splitThread([q] {
|
|
|
19 cmd_t cmd;
|
|
|
20 while (q->get(cmd)) {
|
|
|
21 cmd();
|
|
|
22 }
|
|
|
23 });
|
|
|
24 m_queue = q;
|
|
|
25 });
|
|
|
26 m_queue->put(c);
|
|
|
27 }
|
|
|
28
|
|
|
29 void shutdown() {
|
|
|
30 if (m_queue) {
|
|
|
31 m_queue->set_eof();
|
|
|
32 m_queue.reset();
|
|
|
33 }
|
|
|
34 }
|
|
|
35 ~cmdThread() {
|
|
|
36 shutdown();
|
|
|
37 }
|
|
|
38 private:
|
|
|
39
|
|
|
40 std::shared_ptr< queue_t > m_queue;
|
|
|
41
|
|
|
42
|
|
|
43 std::once_flag m_once;
|
|
|
44 };
|
|
|
45
|
|
|
46 } |