Skip to content

Commit b6555c2

Browse files
committed
refactor(thread): 优化线程池submit函数参数传递
1 parent f4825c5 commit b6555c2

File tree

8 files changed

+16
-16
lines changed

8 files changed

+16
-16
lines changed

hikyuu_cpp/hikyuu/utilities/thread/GlobalMQStealThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ class HKU_UTILS_API GlobalMQStealThreadPool {
104104

105105
/** 向线程池提交任务 */
106106
template <typename FunctionType>
107-
auto submit(FunctionType f) {
107+
auto submit(FunctionType&& f) {
108108
if (m_thread_need_stop.isSet() || m_done) {
109109
throw std::logic_error(
110110
"You can't submit a task to the stopped GlobalMQStealThreadPool!");
111111
}
112112

113113
typedef typename std::invoke_result<FunctionType>::type result_type;
114-
std::packaged_task<result_type()> task(f);
114+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
115115
task_handle<result_type> res(task.get_future());
116116

117117
// 如果是本地线程且线程仍未终止,则加入自身队列

hikyuu_cpp/hikyuu/utilities/thread/GlobalMQThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ class HKU_UTILS_API GlobalMQThreadPool {
103103

104104
/** 向线程池提交任务 */
105105
template <typename FunctionType>
106-
auto submit(FunctionType f) {
106+
auto submit(FunctionType&& f) {
107107
if (m_thread_need_stop.isSet() || m_done) {
108108
throw std::logic_error("You can't submit a task to the stopped GlobalMQThreadPool!");
109109
}
110110

111111
typedef typename std::invoke_result<FunctionType>::type result_type;
112-
std::packaged_task<result_type()> task(f);
112+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
113113
task_handle<result_type> res(task.get_future());
114114

115115
// 向空队列或任务数最小的队列中加入任务

hikyuu_cpp/hikyuu/utilities/thread/GlobalStealThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ class HKU_UTILS_API GlobalStealThreadPool {
158158

159159
/** 向线程池提交任务 */
160160
template <typename FunctionType>
161-
auto submit(FunctionType f) {
161+
auto submit(FunctionType&& f) {
162162
if (m_thread_need_stop.isSet() || m_done.load(std::memory_order_acquire)) {
163163
throw std::logic_error(
164164
"You can't submit a task to the stopped GlobalStealThreadPool!!");
165165
}
166166

167167
typedef typename std::invoke_result<FunctionType>::type result_type;
168-
std::packaged_task<result_type()> task(f);
168+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
169169
task_handle<result_type> res(task.get_future());
170170

171171
std::thread::id id = std::this_thread::get_id();

hikyuu_cpp/hikyuu/utilities/thread/GlobalThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ class HKU_UTILS_API GlobalThreadPool {
9191

9292
/** 向线程池提交任务 */
9393
template <typename FunctionType>
94-
auto submit(FunctionType f) {
94+
auto submit(FunctionType&& f) {
9595
if (m_thread_need_stop.isSet() || m_done) {
9696
throw std::logic_error("You can't submit a task to the stopped task group!");
9797
}
9898
typedef typename std::invoke_result<FunctionType>::type result_type;
99-
std::packaged_task<result_type()> task(f);
99+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
100100
task_handle<result_type> res(task.get_future());
101101
m_master_work_queue.push(std::move(task));
102102
return res;

hikyuu_cpp/hikyuu/utilities/thread/MQStealThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class HKU_UTILS_API MQStealThreadPool {
106106

107107
/** 向线程池提交任务 */
108108
template <typename FunctionType>
109-
auto submit(FunctionType f) {
109+
auto submit(FunctionType&& f) {
110110
if (m_done) {
111111
throw std::logic_error("You can't submit a task to the stopped MQStealThreadPool!");
112112
}
@@ -118,7 +118,7 @@ class HKU_UTILS_API MQStealThreadPool {
118118
}
119119

120120
typedef typename std::invoke_result<FunctionType>::type result_type;
121-
std::packaged_task<result_type()> task(f);
121+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
122122
task_handle<result_type> res(task.get_future());
123123

124124
// 如果是本地线程且线程仍未终止,则加入自身队列

hikyuu_cpp/hikyuu/utilities/thread/MQThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ class HKU_UTILS_API MQThreadPool {
105105

106106
/** 向线程池提交任务 */
107107
template <typename FunctionType>
108-
auto submit(FunctionType f) {
108+
auto submit(FunctionType &&f) {
109109
if (m_done) {
110110
throw std::logic_error("You can't submit a task to the stopped MQThreadPool!");
111111
}
112112

113113
typedef typename std::invoke_result<FunctionType>::type result_type;
114-
std::packaged_task<result_type()> task(f);
114+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
115115
task_handle<result_type> res(task.get_future());
116116

117117
// 向空队列或任务数最小的队列中加入任务

hikyuu_cpp/hikyuu/utilities/thread/StealThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class HKU_UTILS_API StealThreadPool {
109109

110110
/** 向线程池提交任务 */
111111
template <typename FunctionType>
112-
auto submit(FunctionType f) {
112+
auto submit(FunctionType&& f) {
113113
if (m_done) {
114114
throw std::logic_error("You can't submit a task to the stopped StealThreadPool!!");
115115
}
@@ -121,7 +121,7 @@ class HKU_UTILS_API StealThreadPool {
121121
}
122122

123123
typedef typename std::invoke_result<FunctionType>::type result_type;
124-
std::packaged_task<result_type()> task(f);
124+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
125125
task_handle<result_type> res(task.get_future());
126126
if (index != -1 && !m_interrupt_flags[index]) {
127127
// 本地线程任务从前部入队列(递归成栈)

hikyuu_cpp/hikyuu/utilities/thread/ThreadPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ class ThreadPool {
8484

8585
/** 向线程池提交任务 */
8686
template <typename FunctionType>
87-
auto submit(FunctionType f) {
87+
auto submit(FunctionType&& f) {
8888
if (m_done) {
8989
throw std::logic_error("You can't submit a task to the stopped task group!");
9090
}
9191
typedef typename std::invoke_result<FunctionType>::type result_type;
92-
std::packaged_task<result_type()> task(f);
92+
std::packaged_task<result_type()> task(std::forward<FunctionType>(f));
9393
task_handle<result_type> res(task.get_future());
9494
m_master_work_queue.push(std::move(task));
9595
return res;

0 commit comments

Comments
 (0)