当前位置: 首页 > ai >正文

【C/C++】现代C++线程池:从入门到生产级实现

文章目录

  • 现代C++线程池:从入门到生产级实现
    • 🧠 What Is a Thread Pool?
    • 🧩 Why Use a Thread Pool?
    • 🔰 Part 1: Basic Thread Pool (Beginner)
      • 🔧 Minimal Working Code:
      • ✅ Usage:
    • 🧑‍🔬 Part 2: Improving It (Intermediate)
      • 🧵 Add Return Values with `std::future`
    • ⚙️ Part 3: Production-Grade Features (Expert)
      • ✅ Features to Add:
    • 🧵 Part 4: C++20/23 Style Thread Pool
    • 📚 Libraries You Should Know
    • 🧭 Summary

现代C++线程池:从入门到生产级实现

Introduction to thread_pool in modern C++, guiding you through the core ideas and gradually moving toward production-quality implementations. The content is designed to help you deeply understand how thread pools work, and how to write your own using C++17/20/23.


🧠 What Is a Thread Pool?

A thread pool is a collection of pre-spawned threads that wait for tasks to execute. Instead of creating a thread for every task (which is expensive), you reuse a fixed number of threads, each pulling tasks from a task queue.


🧩 Why Use a Thread Pool?

  • ✅ Avoid the overhead of frequent thread creation/destruction.
  • ✅ Reuse a fixed number of threads.
  • ✅ Efficient for high-throughput or I/O-bound systems.
  • ✅ Works well with producer-consumer or event-driven designs.

🔰 Part 1: Basic Thread Pool (Beginner)

A very basic thread pool in C++ using:

  • std::thread
  • std::mutex
  • std::condition_variable
  • std::function
  • std::queue

🔧 Minimal Working Code:

#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <atomic>class ThreadPool {
public:ThreadPool(size_t num_threads);~ThreadPool();void enqueue(std::function<void()> task);private:std::vector<std::thread> workers;std::queue<std::function<void()>> tasks;std::mutex queue_mutex;std::condition_variable condition;std::atomic<bool> stop;
};ThreadPool::ThreadPool(size_t num_threads) : stop(false) {for (size_t i = 0; i < num_threads; ++i) {workers.emplace_back([this]() {while (true) {std::function<void()> task;{std::unique_lock<std::mutex> lock(this->queue_mutex);this->condition.wait(lock, [this]() {return this->stop || !this->tasks.empty();});if (this->stop && this->tasks.empty())return;task = std::move(this->tasks.front());this->tasks.pop();}task(); // run the task}});}
}void ThreadPool::enqueue(std::function<void()> task) {{std::lock_guard<std::mutex> lock(queue_mutex);tasks.push(std::move(task));}condition.notify_one();
}ThreadPool::~ThreadPool() {stop = true;condition.notify_all();for (std::thread &worker : workers)worker.join();
}

✅ Usage:

int main() {ThreadPool pool(4);for (int i = 0; i < 10; ++i) {pool.enqueue([i]() {std::cout << "Running task " << i << " on thread " << std::this_thread::get_id() << "\n";});}std::this_thread::sleep_for(std::chrono::seconds(1));return 0;
}

🧑‍🔬 Part 2: Improving It (Intermediate)

🧵 Add Return Values with std::future

Change enqueue() to return a std::future<T> for each task.

template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)-> std::future<typename std::invoke_result_t<F, Args...>> {using return_type = typename std::invoke_result_t<F, Args...>;auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));std::future<return_type> res = task->get_future();{std::lock_guard<std::mutex> lock(queue_mutex);tasks.emplace([task]() { (*task)(); });}condition.notify_one();return res;
}

Now you can write:

auto future = pool.enqueue([]() {return 42;
});
std::cout << "Result: " << future.get() << "\n";

⚙️ Part 3: Production-Grade Features (Expert)

✅ Features to Add:

FeatureDescription
Dynamic thread resizingIncrease/decrease thread count
Task prioritizationUse std::priority_queue
Shutdown optionsGraceful (drain tasks) vs Immediate
Exception handlingCatch exceptions in tasks
Thread affinity / namingSet thread names or pin to cores
Work stealingFor maximum throughput
Thread-local storageUse thread_local for caches
Integration with coroutines (C++20)Schedule coroutines using the pool

🧵 Part 4: C++20/23 Style Thread Pool

For advanced users, consider using:

  • std::jthread (C++20)
  • std::stop_token
  • std::barrier or std::latch
  • Coroutines (co_await, std::suspend_always)
  • execution::scheduler (C++23 proposal)

Example for C++20 cooperative cancellation:

void worker(std::stop_token stop_token) {while (!stop_token.stop_requested()) {// ...}
}std::jthread t(worker); // can be stopped cleanly

📚 Libraries You Should Know

If you prefer using proven libraries:

LibraryLinkNotes
CTPLEasy-to-use thread pool
BS::thread_poolHeader-only, fast
Boost::asioHeavy but feature-rich
libunifexAdvanced async patterns
follyFacebook’s production async primitives

🧭 Summary

LevelKey Concepts
Beginnerstd::thread, mutex, condition variable, basic queue
Intermediatefutures, exception handling, RAII, std::function, shared task management
Expertstd::jthread, coroutines, scheduling policies, custom allocators, task stealing
http://www.xdnf.cn/news/7562.html

相关文章:

  • power BI 倒计时+插件HTML Content,实现更新倒计时看板!
  • 去中心化算力池:基于IPFS+智能合约的跨校GPU资源共享平台设计
  • 2.4.2死锁的处理策略-预防死锁
  • 【解决】rpm 包安装成功,但目录不存在问题
  • jsmpeg+java+ffmpeg 调用摄像头RTSP流播放
  • DNS 域名解析服务器
  • 卷java,继承三
  • 【Java高阶面经】3.熔断机制深度优化:从抖动治理到微服务高可用架构实战
  • 从Ntfs!NtfsReadMftRecord函数到Ntfs!NtfsMapStream函数从0x274a到0xc4312800
  • SAR ADC 比较器寄生电容对性能的影响
  • 镜像管理(2)Dockerfile总结
  • 技术问答:PHP、JAVA和Go的垃圾回收机制有哪些区别
  • HarmonyOS5云服务技术分享--云函数创建配置指南
  • 软考软件评测师——黑盒测试测试方法
  • python 判断远程windows系统中某进程号是否还在
  • 电商运营数据分析指南之流量指标
  • lambda架构和kappa架构区别
  • 【Unity网络编程知识】协议生成工具Protobuf
  • 05 接口自动化-框架封装思想建立之httprunner框架(中)
  • Qt 控件发展历程 + 目标(1)
  • <uniapp><vuex><状态管理>在uniapp中,如何使用vuex实现数据共享与传递?
  • 基于“岗课赛证”融通的农业物联网专业教学方案
  • Ⅱ 链表 episode3
  • 自回归图像编辑 EditAR: Unified Conditional Generation with Autoregressive Models
  • 力扣第5题:最长回文子串(动态规划)
  • 【全解析】EN18031标准下的NMM网络监控机制
  • css使用clip-path属性切割显示可见内容
  • 【MySQL】第七弹——复习总结 视图
  • SSRF(服务器端请求伪造)基本原理靶场实现
  • CVE-2017-4971源码分析与漏洞复现