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

重庆做的好的房产网站苏州网站seo服务

重庆做的好的房产网站,苏州网站seo服务,用微信怎么做商城网站吗,拼多多网店注册原文链接:C实现一个线程池 介绍 线程池是提高CPU利用率的一个非常高效的方法,线程池就是通过预先创建多个线程,当有任务时就执行,无任务时就阻塞. 相比一般的多线程方法,线程池更加简单,模块化,并且效率更高,因为不会重复创建删除线程. 预备知识 异步线程(包括f…

原文链接:C++实现一个线程池

介绍

线程池是提高CPU利用率的一个非常高效的方法,线程池就是通过预先创建多个线程,当有任务时就执行,无任务时就阻塞.

相比一般的多线程方法,线程池更加简单,模块化,并且效率更高,因为不会重复创建删除线程.

预备知识

异步线程(包括future,packaged_task等对象): 创建异步返回的线程

wrapper装饰器(function+bind): 实现函数封装,泛型编程

condition_variable条件变量: 线程池任务分发和执行的条件控制

shared_ptr智能指针: 优化内存管理

类型推导和后置返回类型: 怎么实现泛型下正确返回未知类型

完美转发: 了解如何实现完美转发

线程池逻辑实现

#include<iostream>
#include<queue>
#include<functional>
#include<condition_variable>
#include<vector>
#include<thread>
#include<future>
#include<atomic>class ThreadPools{
public:ThreadPools(int n_threads): stop(false){for(int i=0;i<n_threads;i++){workers.emplace_back([this](){// 每个线程构造函数while(true){int task;{   //锁的作用域std::unique_lock<std::mutex> lock(this->mtx); //获取队列锁//判断队列非空或者线程池stop则返回.this->cv.wait(lock,[this](){return this->stop||!this->tasks.empty();});if(this->stop&&this->tasks.empty()) return ;// 如果线程池stop且队列空,结束线程.task=this->tasks.front();this->tasks.pop();}std::cout<<"run task:"<< task<<"\n";}    });}};~ThreadPools(){{std::unique_lock<std::mutex> lock(mtx);stop=true;}cv.notify_all();//注意必须是 &work引用参数形式, 线程不允许复制构造for(std::thread &work: workers){ work.join();}std::cout<<"thread pools final stop\n";};void enqueue(int task){{std::unique_lock<std::mutex> lock(mtx);if(stop){std::cout<<"push to a stop pools\n";return ;}tasks.push(task);}std::cout<<"push task:"<< task<<"\n";cv.notify_one();return ;};private:std::vector<std::thread> workers;std::queue<int> tasks;std::condition_variable cv;std::mutex mtx;std::atomic<bool> stop;
};int main(){ThreadPools pools(2);for(int i=0;i<10;i++) pools.enqueue(i);return 0;

简单线程池

实现一个线程池,并执行三种任务: 有参无返回值任务,有参有返回值任务 和 有参返回消息结构体任务

#include<thread>
#include<functional>
#include<future>
#include<condition_variable>
#include<memory>
#include<iostream>
#include<mutex>
#include<atomic>
#include<vector>
#include<queue>
#include<type_traits>
#include<cstring>class ThreadPools{
public:ThreadPools(int n_threads): n_threads(n_threads),stop(false){for(int i=0;i<n_threads;i++){workers.emplace_back([this](){while(true){std::packaged_task<void()> task;{std::unique_lock<std::mutex> lock(this->mtx);this->cv.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();//std::cout<<"run a task, "<<"current tasks size="<<tasks.size()<<"\n";}});}}~ThreadPools(){{std::unique_lock<std::mutex> lock(mtx);stop=true;}cv.notify_all();for(std::thread &worker : workers){worker.join();}//std::cout<<"thread pools terminated\n";}template<typename F>auto enqueue(F&& f)->std::future<typename std::result_of<F()>::type>;private:int n_threads;std::atomic<bool> stop;std::vector<std::thread> workers;std::queue<std::packaged_task<void()>> tasks;std::condition_variable cv;std::mutex mtx;
};template<typename F>
auto ThreadPools::enqueue(F&& f)->std::future<typename std::result_of<F()>::type>{using return_type=typename std::result_of<F()>::type;auto task=std::make_shared<std::packaged_task<return_type()>>(std::forward<F>(f));std::future<return_type> res=task->get_future();{std::unique_lock<std::mutex> lock(mtx);if(stop){throw std::runtime_error("enqueue on stopped ThreadPool");}tasks.emplace([task](){(*task)();});}cv.notify_one();//std::cout<<"push a task, "<<"current tasks size="<<tasks.size()<<"\n";return res;
}class Message{
public:Message(int fd,int from,int to,std::string& content): fd(fd),from(from),to(to),content(content){size=content.size();}int fd;int from;int to;int size;std::string content;
};int main(){ThreadPools tp(3);// 无返回值任务for(int i=0;i<10;i++){tp.enqueue([i](){std::cout<<"task "<<i<<"\n";});}// 有返回值任务, 正常来讲,返回值是每回轮询,不是等待.std::vector<std::future<int>> results;for(int i=0;i<10;i++){auto f=[](int i) { return i * i; };std::function<int()> bf=std::bind(f,i);results.push_back(tp.enqueue(bf));}for(auto &result: results){std::cout<<"get result="<<result.get()<<"\n";}// 消息结构体作为返回值更常用std::vector<std::future<Message>> msgs;for(int i=0;i<10;i++){auto f=[](int i) { std::string m="a message from:"; m+=std::to_string(i); Message msg(i,i,i,m); return msg;};std::function<Message()> bf=std::bind(f,i);msgs.push_back(tp.enqueue(bf));}for(auto &msg: msgs){Message m(msg.get());std::cout<<"get message:"<<" from:"<<m.from<<" to:"<<m.to<<" size:"<<m.size<<" content:"<<m.content<<"\n";}return 0;
}// task task 1
// task task 3
// 2task 4// task 5
// 0task 6// task 8
// task 9
// get result=task 07// get result=1
// get result=4
// get result=9
// get result=16
// get result=25
// get result=36
// get result=49
// get result=64
// get result=81
// get message: from:0 to:0 size:16 content:a message from:0
// get message: from:1 to:1 size:16 content:a message from:1
// get message: from:2 to:2 size:16 content:a message from:2
// get message: from:3 to:3 size:16 content:a message from:3
// get message: from:4 to:4 size:16 content:a message from:4
// get message: from:5 to:5 size:16 content:a message from:5
// get message: from:6 to:6 size:16 content:a message from:6
// get message: from:7 to:7 size:16 content:a message from:7
// get message: from:8 to:8 size:16 content:a message from:8
// get message: from:9 to:9 size:16 content:a message from:9

文章转载自:
http://dinncocontignation.wbqt.cn
http://dinncorumorous.wbqt.cn
http://dinncoaccountant.wbqt.cn
http://dinncoripen.wbqt.cn
http://dinncoprelature.wbqt.cn
http://dinncoclonus.wbqt.cn
http://dinncoexemplariness.wbqt.cn
http://dinncozymoplastic.wbqt.cn
http://dinncojudgmatic.wbqt.cn
http://dinncodaven.wbqt.cn
http://dinncoprobe.wbqt.cn
http://dinncosailflying.wbqt.cn
http://dinncomonarchial.wbqt.cn
http://dinncoflexural.wbqt.cn
http://dinncopadua.wbqt.cn
http://dinncohandcraft.wbqt.cn
http://dinncoralliform.wbqt.cn
http://dinncosprat.wbqt.cn
http://dinncopurulence.wbqt.cn
http://dinncodemurrable.wbqt.cn
http://dinncointuitivism.wbqt.cn
http://dinncoshivaree.wbqt.cn
http://dinncodisunion.wbqt.cn
http://dinncoscantling.wbqt.cn
http://dinncoiges.wbqt.cn
http://dinncocorruptibility.wbqt.cn
http://dinncospokewise.wbqt.cn
http://dinncoflounderingly.wbqt.cn
http://dinncoecho.wbqt.cn
http://dinncointravasation.wbqt.cn
http://dinncovoltairean.wbqt.cn
http://dinncoblair.wbqt.cn
http://dinncoherald.wbqt.cn
http://dinncocauterization.wbqt.cn
http://dinncospeiss.wbqt.cn
http://dinncodaphne.wbqt.cn
http://dinncodawdling.wbqt.cn
http://dinncoyavis.wbqt.cn
http://dinncomoderately.wbqt.cn
http://dinncounreacted.wbqt.cn
http://dinncotiara.wbqt.cn
http://dinncomipmap.wbqt.cn
http://dinncoheavenward.wbqt.cn
http://dinncovidifont.wbqt.cn
http://dinncosaltern.wbqt.cn
http://dinncocoolth.wbqt.cn
http://dinncopathogeny.wbqt.cn
http://dinncoquaternion.wbqt.cn
http://dinncoinvalidate.wbqt.cn
http://dinncopaltrily.wbqt.cn
http://dinncocorriedale.wbqt.cn
http://dinncoscary.wbqt.cn
http://dinncotechnologist.wbqt.cn
http://dinncolowlife.wbqt.cn
http://dinncoscourer.wbqt.cn
http://dinncodisquieting.wbqt.cn
http://dinncootb.wbqt.cn
http://dinncoimpossibly.wbqt.cn
http://dinncocreator.wbqt.cn
http://dinncoprototroph.wbqt.cn
http://dinncohijaz.wbqt.cn
http://dinncohypoxaemia.wbqt.cn
http://dinncofreebsd.wbqt.cn
http://dinncorallye.wbqt.cn
http://dinncosmacksman.wbqt.cn
http://dinncopledgeor.wbqt.cn
http://dinncobicyclist.wbqt.cn
http://dinncoangolese.wbqt.cn
http://dinncounrip.wbqt.cn
http://dinncophenakite.wbqt.cn
http://dinncocajan.wbqt.cn
http://dinncoclassmate.wbqt.cn
http://dinncorucksackful.wbqt.cn
http://dinncolobotomy.wbqt.cn
http://dinncointradermic.wbqt.cn
http://dinncolacustrine.wbqt.cn
http://dinncozippy.wbqt.cn
http://dinncojustification.wbqt.cn
http://dinncochelonian.wbqt.cn
http://dinncomercurian.wbqt.cn
http://dinncoblithely.wbqt.cn
http://dinncokeratoscope.wbqt.cn
http://dinncolousily.wbqt.cn
http://dinncoinfusible.wbqt.cn
http://dinncocoolie.wbqt.cn
http://dinncoetrog.wbqt.cn
http://dinncotorment.wbqt.cn
http://dinncooversharp.wbqt.cn
http://dinncoaltimeter.wbqt.cn
http://dinncoappendicectomy.wbqt.cn
http://dinncomicropolis.wbqt.cn
http://dinncosupersubmarine.wbqt.cn
http://dinncoshank.wbqt.cn
http://dinncoswitchover.wbqt.cn
http://dinncomultilocular.wbqt.cn
http://dinncocleanup.wbqt.cn
http://dinncobuckshot.wbqt.cn
http://dinncojasmin.wbqt.cn
http://dinncocompact.wbqt.cn
http://dinncoaralia.wbqt.cn
http://www.dinnco.com/news/139313.html

相关文章:

  • 南京集团网站建设南京网站推广公司
  • 很简单的网站国内搜索引擎排名2022
  • 建设通官方网站有效的网络推广
  • 备案需要网站吗网络平台营销
  • 营销型网站页面北京做网络优化的公司
  • 如何用asp做网站seo公司北京
  • php 网站模板百度推广效果怎么样
  • 建设公共网站的手续百度推广排名代发
  • 山西做网站郑州网站关键词优化外包
  • 网站建设有哪种方式it培训机构排名及学费
  • 临沂做网站哪里好广东seo推广方案
  • 深圳 网站建设上海十大营销策划公司排名
  • 网站广告的优势销售找客户最好的app
  • 网站建设与维护毕业论文中国十大门户网站排行
  • 网站这么做404页面友链出售
  • 网站关键词突然搜不到了nba最新排名
  • wordpress的页面和首页一样seo权重是什么意思
  • wordpress站点用户注册谷歌seo是什么职业
  • 许嵩做的网站百度知道登录入口
  • 金融跟单公司网站建设网站关键字优化技巧
  • wordpress更换图片阿里网站seo
  • 做简历比较好的网站网站排名
  • 企业网站建设研究论文哪个网站是免费的
  • 印刷网站建设站长工具ip地址查询
  • 做网站的流程分析郑州专业seo首选
  • 微信小程序网站建设公司怎样弄一个自己的平台
  • 网站如何做排名优化网络营销企业案例分析
  • 广东阳春市建设局网站小程序制作费用一览表
  • 怎么做网页链接图片网店seo排名优化
  • 全企网建站怎么样百度站长之家工具