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

网站备案会过期吗广州知名网络推广公司

网站备案会过期吗,广州知名网络推广公司,织梦网站后台管理系统,猫咖网页设计教程目录 实现一个无返回的线程池 完全代码实现 Reference 实现一个无返回的线程池 实现一个简单的线程池非常简单#xff0c;我们首先聊一聊线程池的定义#xff1a; 线程池#xff08;Thread Pool#xff09; 是一种并发编程的设计模式#xff0c;用于管理和复用多个线程…目录 实现一个无返回的线程池 完全代码实现 Reference 实现一个无返回的线程池 实现一个简单的线程池非常简单我们首先聊一聊线程池的定义 线程池Thread Pool 是一种并发编程的设计模式用于管理和复用多个线程以提高程序的性能和资源利用率。它的核心思想是预先创建一组线程并将任务分配给这些线程执行而不是为每个任务单独创建和销毁线程。线程池广泛应用于需要处理大量短期任务的场景例如 Web 服务器、数据库连接池、任务调度系统等。换而言之线程池说白了就是一种饿汉思维——直接预先提供若干的线程由线程池内部控制调度确保我们可以只关心任务的提交以及完成。 我们下面要做的是设计一个任务是不返回的线程池。所以我们约束我们的函数是 using supportive_task_type std::functionvoid(); 下一步就是构造我们的线程池的线程。注意的是——线程和任务是解耦合的意味着我们需要一个中间函数解耦合任务派发。笔者决定将任务派发分到一个私有函数完成 CCThreadPool(const int workers_num) {for(int i 0; i workers_num; i){internal_threads.emplace_back([this](){__scheduled();});}} 上面这个代码很简单就是将每一个线程都分配一个调度函数这个调度函数来委派分发任务办法说简单也很简单 void __scheduled(){while(1){// sources protectionsstd::unique_lockstd::mutex locker(internal_mutex);// waiting for the access of the task resourcescontrolling_cv.wait(locker, [this]{return thread_pool_status || !tasks_queue.empty();});// quit if requriedif(thread_pool_status tasks_queue.empty()){return;}// 现在我们可以取到任务执行了supportive_task_type task(std::move(tasks_queue.front()));tasks_queue.pop();locker.unlock();task();}} 当析构的时候我们也要通知所有线程的cv不要睡眠了由于设置了thread_pool_status是true直接线程跳出来结束全文。 ~CCThreadPool(){thread_pool_status true;controlling_cv.notify_all();for(auto thread : internal_threads){thread.join();}} 完全代码实现 #include condition_variable #include functional #include mutex #include print #include queue #include thread #include utility #include vector ​ class CCThreadPool {public:CCThreadPool()                         delete;CCThreadPool(const CCThreadPool )     delete;CCThreadPool operator(CCThreadPool ) delete; ​CCThreadPool(const int workers_num) {for(int i 0; i workers_num; i){internal_threads.emplace_back([this](){__scheduled();});}} ​~CCThreadPool(){thread_pool_status true;controlling_cv.notify_all();for(auto thread : internal_threads){thread.join();}} ​templatetypename F, typename... Argsvoid enTask(F f, Args... args){supportive_task_type task(std::bind(std::forwardF(f), std::forwardArgs(args)...));{std::unique_lockstd::mutex locker(internal_mutex);tasks_queue.emplace(std::move(task));}controlling_cv.notify_one();} ​private:void __scheduled(){while(1){std::unique_lockstd::mutex locker(internal_mutex);controlling_cv.wait(locker, [this]{return thread_pool_status || !tasks_queue.empty();});// quitif(thread_pool_status tasks_queue.empty()){return;}supportive_task_type task(std::move(tasks_queue.front()));tasks_queue.pop();locker.unlock();task();}} ​using supportive_task_type std::functionvoid();std::vectorstd::thread internal_threads;std::queuesupportive_task_type tasks_queue;std::mutex internal_mutex;std::condition_variable controlling_cv;bool thread_pool_status false; }; ​ ​ int main() {std::println(Task start);CCThreadPool pool(4);for (int i 0; i 8; i) {pool.enTask([i] {std::println(Task {} is started at thread with id {}, i, std::this_thread::get_id());std::this_thread::sleep_for(std::chrono::seconds(1));std::println(Task {} is done, i);});}return 0; } Reference 8. C11 跨平台线程池-See的编程日记 (seestudy.cn)
http://www.hkea.cn/news/14452699/

相关文章:

  • 长沙网站seo费用企业网站asp源代码
  • 国内最大c2c网站上海网站推广多少钱
  • 厦门 网站建设公司电话建站吧网站建设
  • 山西做网站的公司域名解析怎么设置
  • 浙江建设职业技术学院塘栖校区网站从事网站开发
  • wordpress4.8主题南宁网站建设优化服务
  • 网站备案几年备案一次吗一站多通怎么做网站
  • lamp网站开发架构经验wordpress 移动 建站
  • 广东网站建设公司968网站模板制作与安装教程视频
  • 网页设计作业个人网站wordpress无编辑器
  • 网站文章结构变更怎么做301公司宣传推广方案
  • 网站建设 大学生创业网新浪 sae 安装 wordpress 如何上传模板?
  • 中搜网站提交微信小程序个人可以做吗
  • 深圳html5网站制作网站建设与管理是哪个软件
  • 手机微信的网站案例青海网站建设公司多少钱
  • 80端口被封怎么做网站wordpress首页打不开
  • 网站备案审核怎么做qq业务网站
  • 建设手机网站包括哪些费用建设厅网站技术负责人要求
  • 学校网站建设评审会议通知建设什么样的网站月入一万
  • 网站建设标准合同书网页游戏排行榜前十名超清画面
  • 门户网站建设工作领导小组四川省建设局网站
  • 网站建设服务 行业代码在线做图软件
  • 开封建网站呼叫中心网站建设
  • 网站建设logo图片汕头珠宝网站建设
  • 下载了网站模板怎么用西峰住房和城乡建设局网站
  • 公共网站怎地做wordpress is电影主题
  • asp网站发布ftp江苏苏州
  • 工信部网站域名备案查询搜索引擎的工作原理分为
  • 扬州做网站公司关于化妆品网站成功案例
  • 职高网站建设例题做艺术教育类网站如何申请