forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUThreadPool.inl
More file actions
91 lines (73 loc) · 3.22 KB
/
UThreadPool.inl
File metadata and controls
91 lines (73 loc) · 3.22 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: UThreadPool.inl
@Time: 2021/7/4 1:34 下午
@Desc:
***************************/
#ifndef CGRAPH_UTHREADPOOL_INL
#define CGRAPH_UTHREADPOOL_INL
#include "UThreadPool.h"
CGRAPH_NAMESPACE_BEGIN
template<typename FunctionType>
auto UThreadPool::commit(const FunctionType& task, CIndex index)
-> std::future<decltype(std::declval<FunctionType>()())> {
using ResultType = decltype(std::declval<FunctionType>()());
std::packaged_task<ResultType()> runnableTask(std::move(task));
std::future<ResultType> result(runnableTask.get_future());
CIndex realIndex = dispatch(index);
if (realIndex >= 0 && realIndex < config_.default_thread_size_) {
// 如果返回的结果,在主线程数量之间,则放到主线程的queue中执行
primary_threads_[realIndex]->pushTask(std::move(runnableTask));
} else if (CGRAPH_LONG_TIME_TASK_STRATEGY == realIndex) {
/**
* 如果是长时间任务,则交给特定的任务队列,仅由辅助线程处理
* 目的是防止有很多长时间任务,将所有运行的线程均阻塞
* 长任务程序,默认优先级较低
**/
priority_task_queue_.push(std::move(runnableTask), CGRAPH_LONG_TIME_TASK_STRATEGY);
} else {
// 返回其他结果,放到pool的queue中执行
task_queue_.push(std::move(runnableTask));
}
return result;
}
template<typename FunctionType>
auto UThreadPool::commitWithTid(const FunctionType& task, CIndex tid, CBool enable, CBool lockable)
-> std::future<decltype(std::declval<FunctionType>()())> {
using ResultType = decltype(std::declval<FunctionType>()());
std::packaged_task<ResultType()> runnableTask(std::move(task));
std::future<ResultType> result(runnableTask.get_future());
if (tid >= 0 && tid < config_.default_thread_size_) {
primary_threads_[tid]->pushTask(std::move(runnableTask), enable, lockable);
} else {
// 如果超出主线程的范围,则默认写入 pool 通用的任务队列中
task_queue_.push(task);
}
return result;
}
template<typename FunctionType>
auto UThreadPool::commitWithPriority(const FunctionType& task, int priority)
-> std::future<decltype(std::declval<FunctionType>()())> {
using ResultType = decltype(std::declval<FunctionType>()());
std::packaged_task<ResultType()> runnableTask(task);
std::future<ResultType> result(runnableTask.get_future());
if (secondary_threads_.empty()) {
createSecondaryThread(1); // 如果没有开启辅助线程,则直接开启一个
}
priority_task_queue_.push(std::move(runnableTask), priority);
return result;
}
template<typename FunctionType>
CVoid UThreadPool::execute(const FunctionType& task, CIndex index) {
CIndex realIndex = dispatch(index);
if (realIndex >= 0 && realIndex < config_.default_thread_size_) {
primary_threads_[realIndex]->pushTask(std::move(task));
} else if (CGRAPH_LONG_TIME_TASK_STRATEGY == realIndex) {
priority_task_queue_.push(std::move(task), CGRAPH_LONG_TIME_TASK_STRATEGY);
} else {
task_queue_.push(std::move(task));
}
}
CGRAPH_NAMESPACE_END
#endif // CGRAPH_UTHREADPOOL_INL