Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Development Lib > Common Toolkit &&
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
103 Star 575 Fork 242

cpp-master/cpp-tbox

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
develop
Branches (18)
Tags (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
develop
Branches (18)
Tags (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
develop
Branches (18)
Tags (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
cpp-tbox
/
modules
/
eventx
/
thread_pool.cpp
cpp-tbox
/
modules
/
eventx
/
thread_pool.cpp
thread_pool.cpp 14.02 KB
Copy Edit Raw Blame History
海卫哥 authored 2026年04月02日 23:55 +08:00 . feat(base):1.13.9,修改CatchThrow(),添加tag
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "thread_pool.h"
#include <cinttypes>
#include <array>
#include <map>
#include <set>
#include <deque>
#include <thread>
#include <mutex>
#include <algorithm>
#include <condition_variable>
#include <chrono>
#include <tbox/base/log.h>
#include <tbox/base/cabinet.hpp>
#include <tbox/base/assert.h>
#include <tbox/base/catch_throw.h>
#include <tbox/base/object_pool.hpp>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/event/loop.h>
#undef MODULE_ID
#define MODULE_ID "tbox.thread_pool"
namespace tbox {
namespace eventx {
using Clock = std::chrono::steady_clock;
//! ThreadPool 的私有数据
struct ThreadPool::Data {
event::Loop *wp_loop = nullptr; //!< 主线程
bool is_ready = false; //! 是否已经初始化了
size_t min_thread_num = 0; //!< 最少的线程个数
size_t max_thread_num = 0; //!< 最多的线程个数
std::mutex lock; //!< 互斥锁
std::condition_variable cond_var; //!< 条件变量
cabinet::Cabinet<Task> undo_tasks_cabinet;
std::array<std::deque<TaskToken>, THREAD_POOL_PRIO_SIZE> undo_tasks_token; //!< 优先级任务列表,THREAD_POOL_PRIO_SIZE级
std::set<TaskToken> doing_tasks_token; //!< 记录正在从事的任务
size_t idle_thread_num = 0; //!< 空间线程个数
cabinet::Cabinet<std::thread> threads_cabinet;
bool all_threads_stop_flag = false; //!< 是否所有工作线程立即停止标记
size_t undo_task_peak_num_ = 0;
ObjectPool<Task> task_pool{64};
};
/**
* 任务项
*/
struct ThreadPool::Task {
TaskToken token;
NonReturnFunc backend_task; //! 任务在工作线程中执行函数
NonReturnFunc main_cb; //! 任务执行完成后由main_loop执行的回调函数
Clock::time_point create_time_point;
Task *next = nullptr;
};
/////////////////////////////////////////////////////////////////////////////////
ThreadPool::ThreadPool(event::Loop *main_loop) :
d_(new Data)
{
d_->wp_loop = main_loop;
}
ThreadPool::~ThreadPool()
{
if (d_->is_ready)
cleanup();
delete d_;
}
bool ThreadPool::initialize(ssize_t min_thread_num, ssize_t max_thread_num)
{
if (d_->is_ready) {
LogWarn("it has ready, cleanup() first");
return false;
}
if (max_thread_num < 0 || min_thread_num < 0 ||
min_thread_num > max_thread_num || max_thread_num == 0) {
LogWarn("min_thread_num or max_thread_num invalid, min:%d, max:%d", min_thread_num, max_thread_num);
return false;
}
{
std::lock_guard<std::mutex> lg(d_->lock);
d_->min_thread_num = min_thread_num;
d_->max_thread_num = max_thread_num;
for (ssize_t i = 0; i < min_thread_num; ++i)
if (!createWorker())
return false;
}
d_->all_threads_stop_flag = false;
d_->is_ready = true;
return true;
}
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, NonReturnFunc &&main_cb, int prio)
{
RECORD_SCOPE();
TaskToken token;
if (!d_->is_ready) {
LogWarn("need initialize() first");
return token;
}
if (prio < THREAD_POOL_PRIO_MIN)
prio = THREAD_POOL_PRIO_MIN;
else if (prio > THREAD_POOL_PRIO_MAX)
prio = THREAD_POOL_PRIO_MAX;
int level = prio + THREAD_POOL_PRIO_MAX;
{
std::lock_guard<std::mutex> lg(d_->lock);
Task *item = d_->task_pool.alloc();
item->backend_task = std::move(backend_task);
item->main_cb = std::move(main_cb);
item->create_time_point = Clock::now();
item->token = token = d_->undo_tasks_cabinet.alloc(item);
d_->undo_tasks_token.at(level).push_back(token);
//! 如果空闲线程不够分配未认领的任务,且还可以再创建新的线程
if (d_->undo_tasks_cabinet.size() > d_->idle_thread_num) {
if (d_->threads_cabinet.size() < d_->max_thread_num) {
createWorker();
} else {
if (d_->undo_task_peak_num_ < d_->undo_tasks_cabinet.size())
d_->undo_task_peak_num_ = d_->undo_tasks_cabinet.size();
}
}
}
LogDbg("create task %u", token.id());
d_->cond_var.notify_one();
return token;
}
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, const NonReturnFunc &main_cb, int prio)
{
NonReturnFunc backend_task_copy(backend_task);
NonReturnFunc main_cb_copy(main_cb);
return execute(std::move(backend_task_copy), std::move(main_cb_copy), prio);
}
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, int prio)
{
return execute(std::move(backend_task), nullptr, prio);
}
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, int prio)
{
return execute(backend_task, nullptr, prio);
}
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task)
{
return execute(std::move(backend_task), 0);
}
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task)
{
return execute(backend_task, 0);
}
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, NonReturnFunc &&main_cb)
{
return execute(std::move(backend_task), std::move(main_cb), 0);
}
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, const NonReturnFunc &main_cb)
{
return execute(backend_task, main_cb, 0);
}
ThreadExecutor::TaskStatus ThreadPool::getTaskStatus(TaskToken task_token) const
{
std::lock_guard<std::mutex> lg(d_->lock);
if (d_->undo_tasks_cabinet.at(task_token) != nullptr)
return TaskStatus::kWaiting;
if (d_->doing_tasks_token.find(task_token) != d_->doing_tasks_token.end())
return TaskStatus::kExecuting;
return TaskStatus::kNotFound;
}
ThreadExecutor::CancelResult ThreadPool::cancel(TaskToken token)
{
RECORD_SCOPE();
std::lock_guard<std::mutex> lg(d_->lock);
//! 如果正在执行
if (d_->doing_tasks_token.find(token) != d_->doing_tasks_token.end())
return CancelResult::kExecuting; //! 返回正在执行
//! 从高优先级向低优先级遍历,找出优先级最高的任务
for (size_t i = 0; i < d_->undo_tasks_token.size(); ++i) {
auto &tasks_token = d_->undo_tasks_token.at(i);
if (!tasks_token.empty()) {
auto iter = std::find(tasks_token.begin(), tasks_token.end(), token);
if (iter != tasks_token.end()) {
tasks_token.erase(iter);
d_->task_pool.free(d_->undo_tasks_cabinet.free(token));
return CancelResult::kSuccess;
}
}
}
return CancelResult::kNotFound; //! 返回没有找到
}
void ThreadPool::cleanup()
{
RECORD_SCOPE();
if (!d_->is_ready)
return;
std::vector<std::thread*> thread_vec;
{
std::lock_guard<std::mutex> lg(d_->lock);
//! 清空task中的任务
for (size_t i = 0; i < d_->undo_tasks_token.size(); ++i) {
auto &tasks_token = d_->undo_tasks_token.at(i);
while (!tasks_token.empty()) {
auto token = tasks_token.front();
d_->task_pool.free(d_->undo_tasks_cabinet.free(token));
tasks_token.pop_front();
}
}
//! 将threads_cabinet中的线程搬到thread_vec
thread_vec.reserve(d_->threads_cabinet.size());
d_->threads_cabinet.foreach(
[&](std::thread *t) {
thread_vec.push_back(t);
}
);
d_->threads_cabinet.clear();
}
d_->all_threads_stop_flag = true;
d_->cond_var.notify_all();
//! 等待所有的线程退出
for (auto t : thread_vec) {
t->join();
delete t;
}
d_->is_ready = false;
}
ThreadPool::Snapshot ThreadPool::snapshot() const
{
Snapshot ss;
std::lock_guard<std::mutex> lg(d_->lock);
ss.idle_thread_num = d_->idle_thread_num;
ss.thread_num = d_->threads_cabinet.size();
ss.doing_task_num = d_->doing_tasks_token.size();
for (size_t i = 0; i < THREAD_POOL_PRIO_SIZE; ++i)
ss.undo_task_num[i] = d_->undo_tasks_token[i].size();
ss.undo_task_peak_num = d_->undo_task_peak_num_;
return ss;
}
void ThreadPool::threadProc(ThreadToken thread_token)
{
bool let_main_loop_join_me = false;
LogDbg("thread %u start", thread_token.id());
while (true) {
Task* item = nullptr;
{
std::unique_lock<std::mutex> lk(d_->lock);
/**
* 如果当前空闲的线程数量大于等于未被领取的任务数,且当前的线程个数已超过长驻线程数,
* 说明线程数据已满足现有要求则退出当前线程
*/
if ((d_->idle_thread_num >= d_->undo_tasks_cabinet.size()) && (d_->threads_cabinet.size() > d_->min_thread_num)) {
LogDbg("thread %u will exit, no more work.", thread_token.id());
let_main_loop_join_me = true;
break;
}
//! 等待任务
++d_->idle_thread_num;
d_->cond_var.wait(lk, std::bind(&ThreadPool::shouldThreadExitWaiting, this));
--d_->idle_thread_num;
/**
* 有两种情况会从 cond_var.wait() 退出
* 1. 任务队列中有任务需要执行时
* 2. 线程池 cleanup() 时要求所有工作线程退出时
*
* 所以,下面检查 all_threads_stop_flag 看是不是请求退出
*/
if (d_->all_threads_stop_flag) {
LogDbg("thread %u will exit, stop flag.", thread_token.id());
break;
}
item = popOneTask(); //! 从任务队列中取出优先级最高的任务
}
//! 后面就是去执行任务,不需要再加锁了
if (item != nullptr) {
RECORD_SCOPE();
{
std::lock_guard<std::mutex> lg(d_->lock);
d_->doing_tasks_token.insert(item->token);
}
LogDbg("thread %u pick task %u", thread_token.id(), item->token.id());
auto exec_time_point = Clock::now();
auto wait_time_cost = exec_time_point - item->create_time_point;
{
RECORD_SCOPE();
CatchThrow(item->backend_task, "tbox::eventx::ThreadPool", true);
}
auto exec_time_cost = Clock::now() - exec_time_point;
LogDbg("thread %u finish task %u, cost %" PRIu64 " + %" PRIu64 " us",
thread_token.id(), item->token.id(),
wait_time_cost.count() / 1000,
exec_time_cost.count() / 1000);
/**
* 有时在妥托给WorkThread执行动作时,会在lamda中捕获智能指针,它所指向的
* 对象的析构函数是有动作的,如:http的sp_ctx要在析构中发送HTTP回复,如果
* 析构函数在子线程中执行,则会出现不希望见到的多线程竞争。为此,我们在main_cb
* 中也让它持有这个智能指针,希望智能指针所指的对象只在主线程中析构。
*
* 为了保证main_cb中的持有的对象能够在main_loop线程中被析构,
* 所以这里要先task_pool.free(),然后再runInLoop(std::move(main_cpp))
*/
auto main_cb = std::move(item->main_cb);
{
std::lock_guard<std::mutex> lg(d_->lock);
d_->doing_tasks_token.erase(item->token);
d_->task_pool.free(item);
}
if (main_cb) {
RECORD_SCOPE();
d_->wp_loop->runInLoop(std::move(main_cb), "ThreadPool::threadProc, invoke main_cb");
}
}
}
LogDbg("thread %u exit", thread_token.id());
if (let_main_loop_join_me) {
//! 则将线程取出来,交给main_loop去join(),然后delete
std::unique_lock<std::mutex> lk(d_->lock);
auto t = d_->threads_cabinet.free(thread_token);
TBOX_ASSERT(t != nullptr);
d_->wp_loop->runInLoop(
[t]{ t->join(); delete t; },
"ThreadPool::threadProc, join and delete it"
);
//! 这个操作放到最后来做是为了减少主线程join()的等待时长
}
}
bool ThreadPool::createWorker()
{
RECORD_SCOPE();
ThreadToken thread_token = d_->threads_cabinet.alloc();
auto *new_thread = new std::thread(std::bind(&ThreadPool::threadProc, this, thread_token));
if (new_thread != nullptr) {
d_->threads_cabinet.update(thread_token, new_thread);
LogDbg("create thread %u", thread_token.id());
return true;
} else {
LogErr("new thread fail");
return false;
}
}
bool ThreadPool::shouldThreadExitWaiting() const
{
if (d_->all_threads_stop_flag)
return true;
for (size_t i = 0; i < d_->undo_tasks_token.size(); ++i) {
const auto &tasks_token = d_->undo_tasks_token.at(i);
if (!tasks_token.empty()) {
return true;
}
}
return false;
}
ThreadPool::Task* ThreadPool::popOneTask()
{
//! 从高优先级向低优先级遍历,找出优先级最高的任务
for (size_t i = 0; i < d_->undo_tasks_token.size(); ++i) {
auto &tasks_token = d_->undo_tasks_token.at(i);
if (!tasks_token.empty()) {
TaskToken token = tasks_token.front();
tasks_token.pop_front();
return d_->undo_tasks_cabinet.free(token);
}
}
return nullptr;
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

cpp-tbox是一个Linux C++,MIT开源许可的,基于 Reactor 模式的开发框架与组件库。它主要针对智能硬件、机器人、网络服务等开发领域。 它提供了一套易于开发、稳定可靠的框架,有:通信库(TCP/UDP/串口)、HTTP、线程池、定时器池、协程、日志、命令终端、状态机、行为树等非常实用的开发组件,以及完备实用的主框架,能为广大开发者节省大量开发与调试时间。
Cancel

Releases

No release

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cpp-master/cpp-tbox.git
git@gitee.com:cpp-master/cpp-tbox.git
cpp-master
cpp-tbox
cpp-tbox
develop
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /