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

C++11线程池

 使用 condition_variable::wait(unique_lock<mutex>&lck, Predicate pred) 时,必须保证条件变量通过notify唤醒的同时,wait 的第二个参数 Predicate 返回 true 了才可以往下走。必须两个条件同时满足,如果notify的时候Predicate返回时false一样的唤不醒线程。

#include <iostream>
#include <future>
#include <chrono>
#include <string>
#include<functional>
#include <queue>using Task = std::function<void()>;
class ThreaPool
{
public:void start() {running_ = true;}bool is_running(){return running_;}void stop() {std::cout << "stop..." << std::endl;running_ = false;std::unique_lock<std::mutex> lk(qmutext_);tasks_.push([]() { std::cout << "hello pool end " << std::endl;  });cond_.notify_all();}void push_task(Task task) {if (!running_){return;}std::unique_lock<std::mutex> lk(qmutext_);tasks_.push(task);cond_.notify_one();}ThreaPool(int count){for (int i = 0; i < count; i++){pool_.emplace_back([this]() {while (true){Task task;{std::unique_lock<std::mutex> lk(qmutext_);std::cout << "wait 1" << std::endl;cond_.wait(lk, [this] {return !running_ || !tasks_.empty(); });std::cout << "wait 2" << std::endl;if (!tasks_.empty()){task = std::move(tasks_.front());tasks_.pop();}}if (task){task();}if (!this->is_running()){std::cout << "stoped 1" << std::endl;std::unique_lock<std::mutex> lk(qmutext_);if (tasks_.empty()) {std::cout << "stoped 2" << std::endl;return;}}}});}}~ThreaPool() {for (std::thread& worker : pool_) {worker.join();}};private:std::vector<std::thread> pool_;std::mutex qmutext_;std::condition_variable cond_;std::queue< Task > tasks_;std::atomic_bool running_{ true };
};int main()
{ThreaPool pool(5);std::this_thread::sleep_for(std::chrono::seconds(1));pool.push_task([]() { std::cout << "hello pool 1 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 2 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 3 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 4 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 5 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 6 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 7 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 8 \n" << std::endl;  });pool.push_task([]() { std::cout << "hello pool 9 \n" << std::endl;  });std::this_thread::sleep_for(std::chrono::seconds(1));pool.stop();return 0;
}

http://www.lryc.cn/news/197348.html

相关文章:

  • 企业打造VR虚拟展厅,开启商务洽谈新时代!
  • linux部署gitlab
  • c++_learning-基础部分
  • 支持PC端、手机端、数据大屏端的Spring Cloud智慧工地云平台源码
  • 给cmd控制台程序 套壳 美化
  • 【系统架构设计】架构核心知识: 1 构件和中间件
  • 通过开发者工具-网络排查响应时间过长的问题
  • 【Python】Python 实现 Excel 到 CSV 的转换程序
  • BUUCTF题解之[极客大挑战 2019]Havefun 1
  • DIV+CSS网页布局
  • python二次开发CATIA:CATIA Automation
  • 2023年中国云计算软件市场规模、市场结构及市场份额情况分析[图]
  • docker入门加实战—部署Java和前端项目
  • 机器人制作开源方案 | 行星探测车概述
  • Git基础命令
  • C#中Semaphore 和 CountdownEvent 的使用总结
  • THE PLANETS:EARTH vulnhub
  • 【随想】每日两题Day.13
  • CMake Cookbook
  • 钢铁异常分类 few-shot 问题 小陈读paper 钢铁2
  • flask实战(问答平台)
  • RK3568驱动模块编译进内核
  • 黑马程序员Java Web--14.综合案例--修改功能实现
  • 开源协议介绍
  • solidworks 2024新功能之-打造更加智能的工作 硕迪科技
  • Datawhale学习笔记AI +新能源:电动汽车充电站充电量预测
  • 记一次fineBI的增量删除更新BUG
  • rsync+inotify实时同步+双向同步
  • 7.继承与多态 对象村的优质生活
  • 机器视觉、图像处理和计算机视觉:概念和区别