MCPcopy Create free account
hub / github.com/ChunelFeng/CThreadPool / stealTask

Function stealTask

src/UtilsCtrl/ThreadPool/Thread/UThreadPrimary.h:151–178  ·  view source on GitHub ↗

* 从其他线程窃取一个任务 * @param task * @return */

Source from the content-addressed store, hash-verified

149 * @return
150 */
151 bool stealTask(UTaskRef task) {
152 if (unlikely(pool_threads_->size() < config_->default_thread_size_)) {
153 /**
154 * 线程池还未初始化完毕的时候,无法进行steal。
155 * 确保程序安全运行。
156 */
157 return false;
158 }
159
160 /**
161 * 窃取的时候,仅从相邻的primary线程中窃取
162 * 待窃取相邻的数量,不能超过默认primary线程数
163 */
164 int range = config_->calcStealRange();
165 for (int i = 0; i < range; i++) {
166 /**
167 * 从线程中周围的thread中,窃取任务。
168 * 如果成功,则返回true,并且执行任务。
169 */
170 int curIndex = (index_ + i + 1) % config_->default_thread_size_;
171 if (nullptr != (*pool_threads_)[curIndex]
172 && ((*pool_threads_)[curIndex])->work_stealing_queue_.trySteal(task)) {
173 return true;
174 }
175 }
176
177 return false;
178 }
179
180
181 /**

Callers 2

processTaskFunction · 0.85
processTasksFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected