线程池用于管理线程,通过维护预分配的线程池来减少线程开销。具体场景包括:减少线程创建和销毁开销;管理并发性,防止资源耗尽;提高代码简洁性,消除线程管理细节。
C++ 多线程编程中线程池的使用场景
线程池是一种管理线程的机制,它可以提高多线程编程的效率和性能。在 C++ 中,可以通过使用 std::thread 和 std::condition_variable 等标准库来实现线程池。
以下是一些使用线程池的常见场景:
- 减少线程创建和销毁的开销:创建和销毁线程是一个相对昂贵的操作。线程池通过维护一个预先分配的线程池来避免这个开销。
- 管理并发性:线程池可以限制同时执行的线程数量,这对于防止系统资源耗尽非常重要。
- 提高代码简洁性:线程池可以简化多线程编程,因为它消除了管理线程本身的繁琐细节。
实战案例
以下是一个使用 std::thread 和 std::condition_variable 在 C++ 中实现线程池的示例:
#include <condition_variable> #include <iostream> #include <mutex> #include <queue> #include <thread> #include <vector> using namespace std; // 线程池类 class ThreadPool { public: ThreadPool(int num_threads) : stop(false) { // 创建指定数量的线程并将其添加到线程池 for (int i = 0; i < num_threads; i++) { threads.emplace_back([this] { while (!stop) { unique_lock<mutex> lock(mtx); if (!tasks.empty()) { // 从任务队列中获取任务 auto task = tasks.front(); tasks.pop(); // 执行任务 task(); // 通知条件变量任务已完成 cv.notify_one(); } else { // 如果任务队列为空,则等待新任务 cv.wait(lock); } } }); } } ~ThreadPool() { // 停止所有线程 stop = true; cv.notify_all(); // 等待所有线程完成 for (auto& thread : threads) { thread.join(); } } // 向任务队列添加任务 void enqueue(function<void()> &&task) { unique_lock<mutex> lock(mtx); tasks.push(move(task)); // 通知条件变量有新任务 cv.notify_one(); } private: bool stop; // 线程池停止标志 mutex mtx; // 用于保护任务队列和条件变量 condition_variable cv; // 用于等待新任务 queue<function<void()>> tasks; // 任务队列 vector<thread> threads; // 线程池中的线程 }; int main() { // 创建具有 4 个线程的线程池 ThreadPool pool(4); // 向线程池添加 10 个任务 for (int i = 0; i < 10; i++) { pool.enqueue([i] { cout << "Task " << i << " executed by thread " << this_thread::get_id() << endl; }); } return 0; }
登录后复制
在这个例子中,我们创建了一个 ThreadPool 类,它维护一个预先分配的线程池。任务通过 enqueue 函数添加到任务队列中。线程池中的线程持续不断地从任务队列中获取任务并执行它们。当任务队列为空时,线程会等待条件变量被通知,表明有新任务可用。
输出如下所示:
Task 0 executed by thread 139670130218816 Task 1 executed by thread 139670129941952 Task 2 executed by thread 139670130082240 Task 3 executed by thread 139670130226176 Task 4 executed by thread 139670129949696 Task 5 executed by thread 139670130233920 Task 6 executed by thread 139670129957440 Task 7 executed by thread 139670130090080 Task 8 executed by thread 139670130241664 Task 9 executed by thread 139670129965184
登录后复制
以上就是C++ 多线程编程中线程池的使用场景有哪些?的详细内容,更多请关注叮当号网其它相关文章!
文章来自互联网,只做分享使用。发布者:走不完的路,转转请注明出处:https://www.dingdanghao.com/article/529804.html