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

免费外贸网站建设广告软文范例200字

免费外贸网站建设,广告软文范例200字,做中介最好用的网站,网站人员队伍建设薄弱目录 实现一个无返回的线程池 完全代码实现 Reference 实现一个无返回的线程池 实现一个简单的线程池非常简单,我们首先聊一聊线程池的定义: 线程池(Thread Pool) 是一种并发编程的设计模式,用于管理和复用多个线程…

目录

实现一个无返回的线程池

完全代码实现

Reference


实现一个无返回的线程池

实现一个简单的线程池非常简单,我们首先聊一聊线程池的定义:

线程池(Thread Pool) 是一种并发编程的设计模式,用于管理和复用多个线程,以提高程序的性能和资源利用率。它的核心思想是预先创建一组线程,并将任务分配给这些线程执行,而不是为每个任务单独创建和销毁线程。线程池广泛应用于需要处理大量短期任务的场景,例如 Web 服务器、数据库连接池、任务调度系统等。换而言之,线程池说白了就是一种饿汉思维——直接预先提供若干的线程,由线程池内部控制调度,确保我们可以只关心任务的提交以及完成。

我们下面要做的是设计一个任务是不返回的线程池。所以,我们约束我们的函数是:

using supportive_task_type = std::function<void()>;

下一步,就是构造我们的线程池的线程。注意的是——线程和任务是解耦合的,意味着我们需要一个中间函数解耦合任务派发。笔者决定,将任务派发分到一个私有函数完成:

    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_lock<std::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();}}
​template<typename F, typename... Args>void enTask(F&& f, Args&&... args){supportive_task_type task(std::bind(std::forward<F&&>(f), std::forward<Args&&>(args)...));{std::unique_lock<std::mutex> locker(internal_mutex);tasks_queue.emplace(std::move(task));}controlling_cv.notify_one();}
​private:void __scheduled(){while(1){std::unique_lock<std::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::function<void()>;std::vector<std::thread> internal_threads;std::queue<supportive_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. C++11 跨平台线程池-See的编程日记 (seestudy.cn)

http://www.tj-hxxt.cn/news/108981.html

相关文章:

  • 南阳淅川县制作网站的公司湖南正规关键词优化报价
  • 网站全站优化西安优化外包
  • 广西建设行政主管部门官方网站市场营销手段13种手段
  • 诸城网络推广公司重庆企业站seo
  • 抖音添加小程序怎么赚钱seo网络营销公司
  • b2b行业网站程序附近广告公司
  • 备案网站可以做论坛么常州网站建设制作
  • 微信推送在哪个网站做seo优化外包
  • 平面设计公司电话sem优化师是做什么的
  • wordpress 去除底部淘宝网店的seo主要是什么
  • 做网站的都是直男癌吗bt种子万能搜索神器
  • 佛山正规网站建设报价seo建站工具
  • 浙江网缘电子商务有限公司seo自动优化软件安卓
  • 用服务器ip怎么做网站企业软文怎么写
  • python基于web开发的网站开发微信软文案例
  • 手机网站乱弹关键洞察力
  • 做网站要学什么长春模板建站代理
  • 营销网站建设苏州代发新闻稿的网站
  • 购物网站怎么建立舆情服务网站
  • 服装购物网站排名常见的网络营销手段
  • 怎么宣传自己的平台怎样优化网站排名靠前
  • 二手市场网站开发自己怎么做网站推广
  • 建网站的过程宁波seo外包平台
  • wordpress搭建的知名网站搜索引擎排名优化
  • 当今弹幕网站建设情况太原seo管理
  • 电子商务网站建设自建团队宣传软文范例
  • 沈阳网站关键词优化关键词com
  • 做网站的费用进什么科目独立站平台选哪个好
  • 成都市建设厅网站查询网推是什么意思
  • 东莞市官网网站建设公司网站在线推广