hikyuu 2.7.1.1__py3-none-manylinux2014_aarch64.whl → 2.7.2__py3-none-manylinux2014_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. hikyuu/__init__.py +4 -1
  2. hikyuu/__init__.pyi +17 -11
  3. hikyuu/analysis/__init__.pyi +5 -1
  4. hikyuu/analysis/analysis.pyi +6 -2
  5. hikyuu/core.pyi +7 -3
  6. hikyuu/cpp/core310.pyi +49 -6
  7. hikyuu/cpp/core310.so +0 -0
  8. hikyuu/cpp/core311.pyi +49 -6
  9. hikyuu/cpp/core311.so +0 -0
  10. hikyuu/cpp/core312.pyi +49 -6
  11. hikyuu/cpp/core312.so +0 -0
  12. hikyuu/cpp/core313.pyi +49 -6
  13. hikyuu/cpp/core313.so +0 -0
  14. hikyuu/cpp/i18n/zh_CN/hikyuu.mo +0 -0
  15. hikyuu/cpp/i18n/zh_CN/hikyuu_plugin.mo +0 -0
  16. hikyuu/cpp/libhikyuu.so +0 -0
  17. hikyuu/data/clickhouse_upgrade/0001.sql +2 -0
  18. hikyuu/data/common_clickhouse.py +1 -3
  19. hikyuu/data/hku_config_template.py +6 -0
  20. hikyuu/data/mysql_upgrade/0029.sql +2 -0
  21. hikyuu/data/pytdx_to_clickhouse.py +85 -31
  22. hikyuu/data/pytdx_to_h5.py +74 -29
  23. hikyuu/data/pytdx_to_mysql.py +63 -19
  24. hikyuu/data/pytdx_weight_to_clickhouse.py +2 -0
  25. hikyuu/data/pytdx_weight_to_mysql.py +2 -0
  26. hikyuu/data/pytdx_weight_to_sqlite.py +2 -0
  27. hikyuu/data/sqlite_upgrade/0029.sql +4 -0
  28. hikyuu/draw/drawplot/bokeh_draw.pyi +10 -6
  29. hikyuu/draw/drawplot/echarts_draw.pyi +10 -6
  30. hikyuu/draw/drawplot/matplotlib_draw.py +8 -2
  31. hikyuu/draw/drawplot/matplotlib_draw.pyi +10 -6
  32. hikyuu/extend.pyi +6 -2
  33. hikyuu/gui/HikyuuTDX.py +12 -2
  34. hikyuu/gui/data/MainWindow.py +166 -142
  35. hikyuu/hub.pyi +6 -6
  36. hikyuu/include/hikyuu/StockManager.h +4 -0
  37. hikyuu/include/hikyuu/StrategyContext.h +4 -4
  38. hikyuu/include/hikyuu/data_driver/kdata/mysql/MySQLKDataDriver.h +5 -1
  39. hikyuu/include/hikyuu/data_driver/kdata/sqlite/SQLiteKDataDriver.h +1 -1
  40. hikyuu/include/hikyuu/global/sysinfo.h +24 -5
  41. hikyuu/include/hikyuu/indicator/IndicatorImp.h +1 -1
  42. hikyuu/include/hikyuu/plugin/KDataToClickHouseImporter.h +40 -0
  43. hikyuu/include/hikyuu/plugin/KDataToMySQLImporter.h +40 -0
  44. hikyuu/include/hikyuu/plugin/checkdata.h +20 -0
  45. hikyuu/include/hikyuu/plugin/hkuextra.h +2 -0
  46. hikyuu/include/hikyuu/plugin/interface/CheckDataPluginInterface.h +25 -0
  47. hikyuu/include/hikyuu/plugin/interface/HkuExtraPluginInterface.h +2 -0
  48. hikyuu/include/hikyuu/plugin/interface/ImportKDataToClickHousePluginInterface.h +44 -0
  49. hikyuu/include/hikyuu/plugin/interface/ImportKDataToMySQLPluginInterface.h +42 -0
  50. hikyuu/include/hikyuu/plugin/interface/plugins.h +6 -0
  51. hikyuu/include/hikyuu/python/convert_any.h +9 -6
  52. hikyuu/include/hikyuu/strategy/Strategy.h +1 -1
  53. hikyuu/include/hikyuu/trade_manage/TradeRecord.h +2 -1
  54. hikyuu/include/hikyuu/trade_sys/allocatefunds/build_in.h +1 -0
  55. hikyuu/include/hikyuu/trade_sys/allocatefunds/crt/AF_FixedAmount.h +26 -0
  56. hikyuu/include/hikyuu/trade_sys/allocatefunds/imp/FixAmountFunds.h +18 -0
  57. hikyuu/include/hikyuu/utilities/Log.h +6 -7
  58. hikyuu/include/hikyuu/utilities/Parameter.h +17 -0
  59. hikyuu/include/hikyuu/utilities/config.h +28 -0
  60. hikyuu/include/hikyuu/utilities/plugin/PluginBase.h +17 -2
  61. hikyuu/include/hikyuu/utilities/plugin/PluginManager.h +41 -22
  62. hikyuu/include/hikyuu/utilities/thread/GlobalStealThreadPool.h +1 -2
  63. hikyuu/include/hikyuu/utilities/thread/GlobalThreadPool.h +1 -1
  64. hikyuu/include/hikyuu/utilities/thread/MQStealThreadPool.h +285 -0
  65. hikyuu/include/hikyuu/utilities/thread/StealThreadPool.h +296 -0
  66. hikyuu/include/hikyuu/utilities/thread/WorkStealQueue.h +9 -8
  67. hikyuu/include/hikyuu/utilities/thread/algorithm.h +64 -14
  68. hikyuu/include/hikyuu/version.h +4 -4
  69. hikyuu/plugin/libbacktest.so +0 -0
  70. hikyuu/plugin/libcheckdata.so +0 -0
  71. hikyuu/plugin/libclickhousedriver.so +0 -0
  72. hikyuu/plugin/libdataserver.so +0 -0
  73. hikyuu/plugin/libdataserver_parquet.so +0 -0
  74. hikyuu/plugin/libdevice.so +0 -0
  75. hikyuu/plugin/libextind.so +0 -0
  76. hikyuu/plugin/libhkuextra.so +0 -0
  77. hikyuu/plugin/libimport2ch.so +0 -0
  78. hikyuu/plugin/libimport2hdf5.so +0 -0
  79. hikyuu/plugin/libimport2mysql.so +0 -0
  80. hikyuu/plugin/libtmreport.so +0 -0
  81. hikyuu/trade_manage/__init__.pyi +10 -6
  82. hikyuu/trade_manage/trade.pyi +10 -6
  83. hikyuu/trade_sys/trade_sys.py +3 -1
  84. hikyuu/util/singleton.pyi +1 -1
  85. {hikyuu-2.7.1.1.dist-info → hikyuu-2.7.2.dist-info}/METADATA +5 -1
  86. {hikyuu-2.7.1.1.dist-info → hikyuu-2.7.2.dist-info}/RECORD +89 -72
  87. {hikyuu-2.7.1.1.dist-info → hikyuu-2.7.2.dist-info}/WHEEL +0 -0
  88. {hikyuu-2.7.1.1.dist-info → hikyuu-2.7.2.dist-info}/entry_points.txt +0 -0
  89. {hikyuu-2.7.1.1.dist-info → hikyuu-2.7.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,285 @@
1
+ /*
2
+ * StealMQStealThreadPool.h
3
+ *
4
+ * Copyright (c) 2019 hikyuu.org
5
+ *
6
+ * Created on: 2019-9-16
7
+ * Author: fasiondog
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include <future>
13
+ #include <thread>
14
+ #include <chrono>
15
+ #include <vector>
16
+ #include "FuncWrapper.h"
17
+ #include "MQStealQueue.h"
18
+ #include "InterruptFlag.h"
19
+ #include "../cppdef.h"
20
+
21
+ #ifdef __GNUC__
22
+ #pragma GCC diagnostic push
23
+ #pragma GCC diagnostic ignored "-Wsign-compare"
24
+ #endif
25
+ #ifndef HKU_UTILS_API
26
+ #define HKU_UTILS_API
27
+ #endif
28
+
29
+ namespace hku {
30
+
31
+ /**
32
+ * @brief 无集中队列多队列偷取任务池
33
+ * @ingroup ThreadPool
34
+ */
35
+ #ifdef _MSC_VER
36
+ class MQStealThreadPool {
37
+ #else
38
+ class HKU_UTILS_API MQStealThreadPool {
39
+ #endif
40
+ public:
41
+ /**
42
+ * 默认构造函数,创建和当前系统CPU数一致的线程数
43
+ */
44
+ MQStealThreadPool() : MQStealThreadPool(std::thread::hardware_concurrency()) {}
45
+
46
+ /**
47
+ * 构造函数,创建指定数量的线程
48
+ * @param n 指定的线程数
49
+ * @param until_empty 任务队列为空时,自动停止运行
50
+ */
51
+ explicit MQStealThreadPool(size_t n, bool until_empty = true)
52
+ : m_done(false), m_worker_num(n), m_runnging_until_empty(until_empty) {
53
+ try {
54
+ m_interrupt_flags.resize(m_worker_num);
55
+ for (size_t i = 0; i < m_worker_num; i++) {
56
+ // 创建工作线程及其任务队列
57
+ m_queues.emplace_back(new MQStealQueue<task_type>);
58
+ }
59
+ // 初始完毕所有线程资源后再启动线程
60
+ for (int i = 0; i < m_worker_num; i++) {
61
+ m_threads.emplace_back(&MQStealThreadPool::worker_thread, this, i);
62
+ m_thread_index[m_threads.back().get_id()] = i;
63
+ }
64
+ } catch (...) {
65
+ m_done = true;
66
+ throw;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * 析构函数,等待并阻塞至线程池内所有任务完成
72
+ */
73
+ ~MQStealThreadPool() {
74
+ if (!m_done) {
75
+ join();
76
+ }
77
+ }
78
+
79
+ /** 获取工作线程数 */
80
+ size_t worker_num() const {
81
+ return m_worker_num;
82
+ }
83
+
84
+ /** 剩余任务数 */
85
+ size_t remain_task_count() const {
86
+ if (m_done) {
87
+ return 0;
88
+ }
89
+
90
+ size_t total = 0;
91
+ for (size_t i = 0; i < m_worker_num; i++) {
92
+ total += m_queues[i]->size();
93
+ }
94
+ return total;
95
+ }
96
+
97
+ /** 先线程池提交任务后返回的对应 future 的类型 */
98
+ template <typename ResultType>
99
+ using task_handle = std::future<ResultType>;
100
+
101
+ #ifdef _MSC_VER
102
+ #pragma warning(push)
103
+ #pragma warning(disable : 4996)
104
+ #endif
105
+
106
+ /** 向线程池提交任务 */
107
+ template <typename FunctionType>
108
+ auto submit(FunctionType f) {
109
+ if (m_done) {
110
+ throw std::logic_error("You can't submit a task to the stopped MQStealThreadPool!");
111
+ }
112
+
113
+ int index = -1;
114
+ auto iter = m_thread_index.find(std::this_thread::get_id());
115
+ if (iter != m_thread_index.end()) {
116
+ index = iter->second;
117
+ }
118
+
119
+ typedef typename std::invoke_result<FunctionType>::type result_type;
120
+ std::packaged_task<result_type()> task(f);
121
+ task_handle<result_type> res(task.get_future());
122
+
123
+ // 如果是本地线程且线程仍未终止,则加入自身队列
124
+ if (index != -1 && m_interrupt_flags[index]) {
125
+ // 本地线程任务从前部入队列(递归成栈)
126
+ m_queues[index]->push_front(std::move(task));
127
+ return res;
128
+ }
129
+
130
+ m_queues[m_current_index]->push(std::move(task));
131
+ m_current_index++;
132
+ if (m_current_index >= m_worker_num) {
133
+ m_current_index = 0;
134
+ }
135
+ return res;
136
+ }
137
+
138
+ #ifdef _MSC_VER
139
+ #pragma warning(pop)
140
+ #endif
141
+
142
+ /** 返回线程池结束状态 */
143
+ bool done() const {
144
+ return m_done;
145
+ }
146
+
147
+ /**
148
+ * 等待各线程完成当前执行的任务后立即结束退出
149
+ */
150
+ void stop() {
151
+ if (m_done) {
152
+ return;
153
+ }
154
+
155
+ // 同时加入结束任务指示,以便在dll退出时也能够终止
156
+ for (size_t i = 0; i < m_worker_num; i++) {
157
+ if (m_interrupt_flags[i]) {
158
+ m_interrupt_flags[i].set();
159
+ }
160
+ m_queues[i]->push(FuncWrapper());
161
+ }
162
+
163
+ for (size_t i = 0; i < m_worker_num; i++) {
164
+ if (m_threads[i].joinable()) {
165
+ m_threads[i].join();
166
+ }
167
+ }
168
+
169
+ m_done = true;
170
+ for (size_t i = 0; i < m_worker_num; i++) {
171
+ m_queues[i]->clear();
172
+ }
173
+ }
174
+
175
+ /**
176
+ * 等待并阻塞至线程池内所有任务完成
177
+ * @note 至此线程池能工作线程结束不可再使用
178
+ */
179
+ void join() {
180
+ if (m_done) {
181
+ return;
182
+ }
183
+
184
+ // 指示各工作线程在未获取到工作任务时,停止运行
185
+ if (m_runnging_until_empty) {
186
+ while (true) {
187
+ bool can_quit = true;
188
+ for (size_t i = 0; i < m_worker_num; i++) {
189
+ if (!m_queues[i]->empty()) {
190
+ can_quit = false;
191
+ break;
192
+ }
193
+ }
194
+
195
+ if (can_quit) {
196
+ break;
197
+ }
198
+
199
+ std::this_thread::yield();
200
+ }
201
+
202
+ m_done = true;
203
+ for (size_t i = 0; i < m_worker_num; i++) {
204
+ if (m_interrupt_flags[i]) {
205
+ m_interrupt_flags[i].set();
206
+ }
207
+ }
208
+ }
209
+
210
+ for (size_t i = 0; i < m_worker_num; i++) {
211
+ m_queues[i]->push(FuncWrapper());
212
+ }
213
+
214
+ // 等待线程结束
215
+ for (size_t i = 0; i < m_worker_num; i++) {
216
+ if (m_threads[i].joinable()) {
217
+ m_threads[i].join();
218
+ }
219
+ }
220
+
221
+ m_done = true;
222
+ for (size_t i = 0; i < m_worker_num; i++) {
223
+ m_queues[i]->clear();
224
+ }
225
+ }
226
+
227
+ private:
228
+ typedef FuncWrapper task_type;
229
+ std::atomic_bool m_done; // 线程池全局需终止指示
230
+ size_t m_worker_num; // 工作线程数量
231
+ bool m_runnging_until_empty; // 运行直到队列空时停止
232
+
233
+ std::vector<std::unique_ptr<MQStealQueue<task_type>>> m_queues; // 线程任务队列
234
+ std::vector<InterruptFlag> m_interrupt_flags; // 线程终止标志
235
+ std::vector<std::thread> m_threads; // 工作线程
236
+
237
+ std::unordered_map<std::thread::id, int> m_thread_index;
238
+ int m_current_index = 0; // 当前放置新任务时的队列索引
239
+
240
+ void worker_thread(int index) {
241
+ while (!m_interrupt_flags[index].isSet() && !m_done) {
242
+ run_pending_task(index);
243
+ }
244
+ }
245
+
246
+ void run_pending_task(int index) {
247
+ task_type task;
248
+ // 尝试从本地队列获取任务并执行
249
+ if (m_queues[index]->try_pop(task)) {
250
+ if (task.isNullTask()) {
251
+ m_interrupt_flags[index].set();
252
+ } else {
253
+ task();
254
+ }
255
+ } else if (pop_task_from_other_thread_queue(task, index)) {
256
+ task();
257
+ } else {
258
+ // 阻塞并等待本地队列中有新的任务
259
+ // 注:如果是递归情况,任务被优先加入本地队列且彼此依赖,如果任务被其他线程偷取到的话,将导致其他线程被阻塞并等待
260
+ // 所以,此处等待本地队列而不是继续循环偷取
261
+ m_queues[index]->wait_and_pop(task);
262
+ if (task.isNullTask()) {
263
+ m_interrupt_flags[index].set();
264
+ } else {
265
+ task();
266
+ }
267
+ }
268
+ }
269
+
270
+ bool pop_task_from_other_thread_queue(task_type& task, int index) {
271
+ for (size_t i = 0; i < m_worker_num; ++i) {
272
+ size_t pos = (index + i + 1) % m_worker_num;
273
+ if (pos != index && m_queues[pos]->try_steal(task)) {
274
+ return true;
275
+ }
276
+ }
277
+ return false;
278
+ }
279
+ }; // namespace hku
280
+
281
+ } /* namespace hku */
282
+
283
+ #ifdef __GNUC__
284
+ #pragma GCC diagnostic pop
285
+ #endif
@@ -0,0 +1,296 @@
1
+ /*
2
+ * StealThreadPool.h
3
+ *
4
+ * Copyright (c) 2019 hikyuu.org
5
+ *
6
+ * Created on: 2019-9-16
7
+ * Author: fasiondog
8
+ */
9
+
10
+ #pragma once
11
+ #ifndef HIKYUU_UTILITIES_THREAD_STEALTHREADPOOL_H
12
+ #define HIKYUU_UTILITIES_THREAD_STEALTHREADPOOL_H
13
+
14
+ #include <future>
15
+ #include <thread>
16
+ #include <vector>
17
+ #include "ThreadSafeQueue.h"
18
+ #include "WorkStealQueue.h"
19
+ #include "InterruptFlag.h"
20
+ #include "../cppdef.h"
21
+
22
+ #ifdef __GNUC__
23
+ #pragma GCC diagnostic push
24
+ #pragma GCC diagnostic ignored "-Wsign-compare"
25
+ #endif
26
+
27
+ #ifndef HKU_UTILS_API
28
+ #define HKU_UTILS_API
29
+ #endif
30
+
31
+ namespace hku {
32
+
33
+ /**
34
+ * @brief 分布偷取式线程池
35
+ * @note 主要用于存在递归情况,任务又创建任务加入线程池的情况,否则建议使用普通的线程池
36
+ * @details
37
+ * @ingroup ThreadPool
38
+ */
39
+ #ifdef _MSC_VER
40
+ class StealThreadPool {
41
+ #else
42
+ class HKU_UTILS_API StealThreadPool {
43
+ #endif
44
+ public:
45
+ /**
46
+ * 默认构造函数,创建和当前系统CPU数一致的线程数
47
+ */
48
+ StealThreadPool() : StealThreadPool(std::thread::hardware_concurrency()) {}
49
+
50
+ /**
51
+ * 构造函数,创建指定数量的线程
52
+ * @param n 指定的线程数
53
+ * @param until_empty 任务队列为空时,自动停止运行
54
+ */
55
+ explicit StealThreadPool(size_t n, bool until_empty = true)
56
+ : m_done(false), m_worker_num(n), m_running_until_empty(until_empty) {
57
+ try {
58
+ m_interrupt_flags.resize(m_worker_num);
59
+ for (int i = 0; i < m_worker_num; i++) {
60
+ // 创建工作线程及其任务队列
61
+ m_queues.emplace_back(new WorkStealQueue);
62
+ }
63
+ // 初始完毕所有线程资源后再启动线程
64
+ for (int i = 0; i < m_worker_num; i++) {
65
+ m_threads.emplace_back(&StealThreadPool::worker_thread, this, i);
66
+ m_thread_index[m_threads.back().get_id()] = i;
67
+ }
68
+ } catch (...) {
69
+ m_done = true;
70
+ throw;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * 析构函数,等待并阻塞至线程池内所有任务完成
76
+ */
77
+ ~StealThreadPool() {
78
+ if (!m_done) {
79
+ join();
80
+ }
81
+ }
82
+
83
+ /** 获取工作线程数 */
84
+ size_t worker_num() const {
85
+ return m_worker_num;
86
+ }
87
+
88
+ /** 剩余任务数 */
89
+ size_t remain_task_count() const {
90
+ if (m_done) {
91
+ return 0;
92
+ }
93
+ size_t total = m_master_work_queue.size();
94
+ for (size_t i = 0; i < m_worker_num; i++) {
95
+ total += m_queues[i]->size();
96
+ }
97
+ return total;
98
+ }
99
+
100
+ /** 先线程池提交任务后返回的对应 future 的类型 */
101
+ template <typename ResultType>
102
+ using task_handle = std::future<ResultType>;
103
+
104
+ #ifdef _MSC_VER
105
+ #pragma warning(push)
106
+ #pragma warning(disable : 4996)
107
+ #endif
108
+
109
+ /** 向线程池提交任务 */
110
+ template <typename FunctionType>
111
+ auto submit(FunctionType f) {
112
+ if (m_done) {
113
+ throw std::logic_error("You can't submit a task to the stopped StealThreadPool!!");
114
+ }
115
+
116
+ int index = -1;
117
+ auto iter = m_thread_index.find(std::this_thread::get_id());
118
+ if (iter != m_thread_index.end()) {
119
+ index = iter->second;
120
+ }
121
+
122
+ typedef typename std::invoke_result<FunctionType>::type result_type;
123
+ std::packaged_task<result_type()> task(f);
124
+ task_handle<result_type> res(task.get_future());
125
+ if (index != -1 && !m_interrupt_flags[index]) {
126
+ // 本地线程任务从前部入队列(递归成栈)
127
+ m_queues[index]->push_front(std::move(task));
128
+ } else {
129
+ m_master_work_queue.push(std::move(task));
130
+ m_cv.notify_one();
131
+ }
132
+ return res;
133
+ }
134
+
135
+ #ifdef _MSC_VER
136
+ #pragma warning(pop)
137
+ #endif
138
+
139
+ /** 返回线程池结束状态 */
140
+ bool done() const {
141
+ return m_done;
142
+ }
143
+
144
+ /**
145
+ * 等待各线程完成当前执行的任务后立即结束退出
146
+ */
147
+ void stop() {
148
+ if (m_done) {
149
+ return;
150
+ }
151
+
152
+ m_done = true;
153
+
154
+ // 同时加入结束任务指示,以便在dll退出时也能够终止
155
+ for (size_t i = 0; i < m_worker_num; i++) {
156
+ m_interrupt_flags[i].set();
157
+ m_queues[i]->push_front(FuncWrapper());
158
+ }
159
+
160
+ m_cv.notify_all(); // 唤醒所有工作线程
161
+ for (size_t i = 0; i < m_worker_num; i++) {
162
+ if (m_threads[i].joinable()) {
163
+ m_threads[i].join();
164
+ }
165
+ }
166
+
167
+ m_master_work_queue.clear();
168
+ for (size_t i = 0; i < m_worker_num; i++) {
169
+ m_queues[i]->clear();
170
+ }
171
+ }
172
+
173
+ /**
174
+ * 等待并阻塞至线程池内所有任务完成
175
+ * @note 至此线程池能工作线程结束不可再使用
176
+ */
177
+ void join() {
178
+ if (m_done) {
179
+ return;
180
+ }
181
+
182
+ // 指示各工作线程在未获取到工作任务时,停止运行
183
+ if (m_running_until_empty) {
184
+ while (true) {
185
+ if (m_master_work_queue.size() != 0) {
186
+ std::this_thread::yield();
187
+ } else {
188
+ bool can_quit = true;
189
+ for (size_t i = 0; i < m_worker_num; i++) {
190
+ if (!m_queues[i]->empty()) {
191
+ can_quit = false;
192
+ break;
193
+ }
194
+ }
195
+ if (can_quit) {
196
+ break;
197
+ } else {
198
+ std::this_thread::yield();
199
+ }
200
+ }
201
+ }
202
+
203
+ m_done = true;
204
+ for (size_t i = 0; i < m_worker_num; i++) {
205
+ m_interrupt_flags[i].set();
206
+ }
207
+ }
208
+
209
+ for (size_t i = 0; i < m_worker_num; i++) {
210
+ m_master_work_queue.push(FuncWrapper());
211
+ }
212
+
213
+ // 唤醒所有工作线程
214
+ m_cv.notify_all();
215
+
216
+ // 等待线程结束
217
+ for (size_t i = 0; i < m_worker_num; i++) {
218
+ if (m_threads[i].joinable()) {
219
+ m_threads[i].join();
220
+ }
221
+ }
222
+
223
+ m_done = true;
224
+ m_master_work_queue.clear();
225
+ for (size_t i = 0; i < m_worker_num; i++) {
226
+ m_queues[i]->clear();
227
+ }
228
+ }
229
+
230
+ private:
231
+ typedef FuncWrapper task_type;
232
+ std::atomic_bool m_done; // 线程池全局需终止指示
233
+ size_t m_worker_num; // 工作线程数量
234
+ bool m_running_until_empty; // 任务队列为空时,自动停止运行
235
+ std::condition_variable m_cv; // 信号量,无任务时阻塞线程并等待
236
+ std::mutex m_cv_mutex; // 配合信号量的互斥量
237
+
238
+ std::vector<InterruptFlag> m_interrupt_flags; // 工作线程状态
239
+ ThreadSafeQueue<task_type> m_master_work_queue; // 主线程任务队列
240
+ std::vector<std::unique_ptr<WorkStealQueue>> m_queues; // 任务队列(每个工作线程一个)
241
+ std::vector<std::thread> m_threads; // 工作线程
242
+ std::unordered_map<std::thread::id, int> m_thread_index;
243
+
244
+ void worker_thread(int index) {
245
+ while (!m_done && !m_interrupt_flags[index]) {
246
+ run_pending_task(index);
247
+ }
248
+ }
249
+
250
+ void run_pending_task(int index) {
251
+ // 从本地队列提前工作任务,如本地无任务则从主队列中提取任务
252
+ // 如果主队列中提取的任务是空任务,则认为需结束本线程,否则从其他工作队列中偷取任务
253
+ task_type task;
254
+ if (pop_task_from_local_queue(task, index)) {
255
+ if (!task.isNullTask()) {
256
+ task();
257
+ } else {
258
+ m_interrupt_flags[index].set();
259
+ }
260
+ } else if (pop_task_from_master_queue(task)) {
261
+ if (!task.isNullTask()) {
262
+ task();
263
+ } else {
264
+ m_interrupt_flags[index].set();
265
+ }
266
+ } else if (pop_task_from_other_thread_queue(task, index)) {
267
+ task();
268
+ } else {
269
+ std::unique_lock<std::mutex> lk(m_cv_mutex);
270
+ m_cv.wait(lk, [this] { return this->m_done || !this->m_master_work_queue.empty(); });
271
+ }
272
+ }
273
+
274
+ bool pop_task_from_master_queue(task_type& task) {
275
+ return m_master_work_queue.try_pop(task);
276
+ }
277
+
278
+ // cppcheck-suppress functionStatic // 屏蔽cppcheck转静态函数建议
279
+ bool pop_task_from_local_queue(task_type& task, int index) {
280
+ return m_queues[index]->try_pop(task);
281
+ }
282
+
283
+ bool pop_task_from_other_thread_queue(task_type& task, int index) {
284
+ for (int i = 0; i < m_worker_num; ++i) {
285
+ int pos = (index + i + 1) % m_worker_num;
286
+ if (pos != index && !m_interrupt_flags[pos] && m_queues[pos]->try_steal(task)) {
287
+ return true;
288
+ }
289
+ }
290
+ return false;
291
+ }
292
+ };
293
+
294
+ } /* namespace hku */
295
+
296
+ #endif /* HIKYUU_UTILITIES_THREAD_STEALTHREADPOOL_H */
@@ -12,7 +12,7 @@
12
12
  #define HIKYUU_UTILITIES_THREAD_WORKSTEALQUEUE_H
13
13
 
14
14
  #include <deque>
15
- #include <mutex>
15
+ #include <shared_mutex>
16
16
  #include "FuncWrapper.h"
17
17
 
18
18
  namespace hku {
@@ -24,7 +24,7 @@ class WorkStealQueue {
24
24
  private:
25
25
  typedef FuncWrapper data_type;
26
26
  std::deque<data_type> m_queue;
27
- mutable std::mutex m_mutex;
27
+ mutable std::shared_mutex m_mutex;
28
28
 
29
29
  public:
30
30
  /** 构造函数 */
@@ -36,29 +36,30 @@ public:
36
36
 
37
37
  /** 将数据插入队列头部 */
38
38
  void push_front(data_type&& data) {
39
- std::lock_guard<std::mutex> lock(m_mutex);
39
+ std::unique_lock<std::shared_mutex> lock(m_mutex);
40
40
  m_queue.push_front(std::move(data));
41
41
  }
42
42
 
43
43
  /** 将数据插入队列尾部 */
44
44
  void push_back(data_type&& data) {
45
- std::lock_guard<std::mutex> lock(m_mutex);
45
+ std::unique_lock<std::shared_mutex> lock(m_mutex);
46
46
  m_queue.push_back(std::move(data));
47
47
  }
48
48
 
49
49
  /** 队列是否为空 */
50
50
  bool empty() const {
51
- std::lock_guard<std::mutex> lock(m_mutex);
51
+ std::shared_lock<std::shared_mutex> lock(m_mutex);
52
52
  return m_queue.empty();
53
53
  }
54
54
 
55
55
  /** 队列大小,!未加锁,谨慎使用 */
56
56
  size_t size() const {
57
+ std::shared_lock<std::shared_mutex> lock(m_mutex);
57
58
  return m_queue.size();
58
59
  }
59
60
 
60
61
  void clear() {
61
- std::lock_guard<std::mutex> lock(m_mutex);
62
+ std::unique_lock<std::shared_mutex> lock(m_mutex);
62
63
  auto tmp = std::deque<data_type>();
63
64
  m_queue.swap(tmp);
64
65
  }
@@ -69,7 +70,7 @@ public:
69
70
  * @return 如果原本队列为空返回 false,否则为 true
70
71
  */
71
72
  bool try_pop(data_type& res) {
72
- std::lock_guard<std::mutex> lock(m_mutex);
73
+ std::unique_lock<std::shared_mutex> lock(m_mutex);
73
74
  if (m_queue.empty()) {
74
75
  return false;
75
76
  }
@@ -85,7 +86,7 @@ public:
85
86
  * @return 如果原本队列为空返回 false,否则为 true
86
87
  */
87
88
  bool try_steal(data_type& res) {
88
- std::lock_guard<std::mutex> lock(m_mutex);
89
+ std::unique_lock<std::shared_mutex> lock(m_mutex);
89
90
  if (m_queue.empty()) {
90
91
  return false;
91
92
  }