-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_manager.hpp
78 lines (58 loc) · 1.56 KB
/
async_manager.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef ASYNC_MANAGER_HPP_INCLUDED
#define ASYNC_MANAGER_HPP_INCLUDED
#include <functional>
namespace AsyncManager
{
typedef std::function<void()> Task;
/**
Run given task asynchronously in background worker thread.
- `group` specifies a group name for this task. See @ref sync(const char*);
- `allowQueue` specifies if the task can be just put into the queue. If not
then caller is blocked until the task is dequeued by a worker thread;
- `task` holds task body.
Total number of concurrent workers can be limited to improve performance.
*/
void async(const char* group, bool allowQueue, const Task& task);
/**
@overload
*/
inline void async(const char* group, const Task& task)
{
async(group, true, task);
}
/**
@overload
*/
inline void async(bool allowQueue, const Task& task)
{
async("", allowQueue, task);
}
/**
@overload
*/
inline void async(const Task& task)
{
async("", task);
}
/**
Synchronize to asynchronous tasks in the given group.
Caller is blocked until all asynchronous tasks in the group are finished.
*/
void sync(const char* group);
/**
Synchronize to all asynchronous tasks.
Caller is blocked until all synchronous and asynchronous tasks are finished.
*/
void syncAll();
/**
Run given task when tick() is called.
*/
void sync(const Task& task);
/**
Run all queued synchronous tasks.
Tasks are run in the same order they were added.
This function is supposed to be run periodically from main thread.
*/
void tick();
}
#endif