forked from tonttu/pledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManualExecutor.hpp
43 lines (36 loc) · 926 Bytes
/
ManualExecutor.hpp
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
#pragma once
#include <mutex>
#include <vector>
#include "Executor.hpp"
namespace Pledge {
// An executor that adds tasks to a queue that needs to be manually executed by
// calling run().
//
// Multithreaded applications could have these in various threads and different
// thread event loops or main loops could call run() on them periodically, so
// you could easily write continuations that jump between relevant threads in
// the application.
class ManualExecutor : public Executor
{
public:
inline void add(Func func) override
{
std::lock_guard<std::mutex> g(m_queueMutex);
m_queue.push_back(std::move(func));
}
inline size_t run()
{
std::vector<Func> todo;
{
std::unique_lock<std::mutex> lock(m_queueMutex);
std::swap(todo, m_queue);
}
for (Func& f : todo)
f();
return todo.size();
}
private:
std::vector<Func> m_queue;
std::mutex m_queueMutex;
};
}