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

公司网站的备案号是如何链接的农村自建房设计图及效果图

公司网站的备案号是如何链接的,农村自建房设计图及效果图,分站城市网站如何做seo,星辰博客wordpress原理说明#xff1a; 1. 线程池创建时#xff0c;指定线程池的大小thread_size。当有新的函数任务通过函数addFunction ()添加进来后#xff0c;其中一个线程执行函数。一个线程一次执行一个函数。如果函数数量大与线程池数量#xff0c;则后来的函数等待。 2. 线程池内部…原理说明 1. 线程池创建时指定线程池的大小thread_size。当有新的函数任务通过函数addFunction ()添加进来后其中一个线程执行函数。一个线程一次执行一个函数。如果函数数量大与线程池数量则后来的函数等待。 2. 线程池内部有个容器m_functions 来存储待执行的函数。函数执行后从队列中移除。 3.  stopAll()函数会停止线程池。 ThreadPool.h //ThreadPool.h #include condition_variable #include cstddef #include functional #include future #include memory #include mutex #include queue #include thread #include vectorclass ThreadPool { public:static ThreadPool* getInstance(size_t thread_size 1); //默认线程池大小void addFunction(std::functionvoid() task); //添加需要执行的函数~ThreadPool();void stopAll(bool immediately); //停止线程池 immediately:true立即停止 immediately:false等待当前线程函数执行完后停止。 private:ThreadPool(size_t thread_size);private:void workerThreadHandler();std::vectorstd::thread m_workers; //线程容器std::queuestd::functionvoid() m_functions; //待执行的函数容器std::mutex m_queue_mutex;std::condition_variable m_condition;bool m_stop; //线程池停止状态std::thread m_wakeTimerThread;std::mutex m_timer_mutex; };ThreadPool.cpp #include ThreadPool.h #include chrono #include memory #include thread #include pthread.h #include iostream using namespace std;ThreadPool* ThreadPool::getInstance(size_t thread_size) {static std::mutex m_lock;static std::shared_ptrThreadPool m_instancenullptr;if (nullptr m_instance){m_instance.reset(new ThreadPool(thread_size));}return m_instance.get(); } ThreadPool::ThreadPool(size_t thread_size) : m_stop(false){m_workers.reserve(thread_size);for (size_t i0; ithread_size; i){m_workers.emplace_back([this, i](){ //创建线程池中的线程workerThreadHandler();});}//辅助线程每隔一段时间发送一次唤醒防止线程阻塞m_wakeTimerThread std::thread([this](){ for(;!this-m_stop;){std::unique_lockstd::mutex lock(this-m_timer_mutex);std::this_thread::sleep_for(std::chrono::milliseconds(2000));pthread_testcancel();m_condition.notify_all();std::coutwake upstd::endl;}});std::cout__func__std::endl; }//线程循环函数循环查询函数容器是否为空不为空则读取一个函数并执行。 void ThreadPool::workerThreadHandler() {for (;!this-m_stop;){std::functionvoid() task;{std::unique_lockstd::mutex lock(this-m_queue_mutex);std::couttasks begin size:this-m_functions.size() stop:m_stopstd::endl;if (!m_stop m_functions.empty()){this-m_condition.wait(lock);}pthread_testcancel(); //作为线程的终止点if (this-m_stop){return;}if (this-m_functions.empty()){continue;}task std::move(this-m_functions.front());this-m_functions.pop();std::coutstd::this_thread::get_id() tasks end size:this-m_functions.size()std::endl;}task();std::cout__func__ end taskstd::endl;std::this_thread::sleep_for(std::chrono::milliseconds(200));} }void ThreadPool::addFunction(std::functionvoid() task) //添加待执行的函数 {std::unique_lockstd::mutex lock(m_queue_mutex);if (m_stop){return;}m_functions.emplace(std::move(task));m_condition.notify_one(); }ThreadPool::~ThreadPool() {{std::unique_lockstd::mutex lock(m_queue_mutex);m_stop true;}m_condition.notify_all();for (std::thread worker: m_workers){worker.join();}m_wakeTimerThread.join();std::cout__func__std::endl; }void ThreadPool::stopAll(bool immediately) //停止线程 immediately:true立即停止 {this-m_stop true;if (immediately){for (std::thread worker: m_workers){pthread_cancel(worker.native_handle());}pthread_cancel(m_wakeTimerThread.native_handle());} }测试程序main.cpp #include iostream #include chrono #include mutex #include ThreadPool.husing namespace std;static std::mutex m_mutex; void ProcessFunc111() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout__func__ endstd::endl; }void ProcessFunc222() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc333() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc444() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc555() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc666() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout__func__ endstd::endl; }int main() {ThreadPool::getInstance(5);ThreadPool::getInstance()-addFunction([](){ProcessFunc111();});ThreadPool::getInstance()-addFunction([](){ProcessFunc222();});ThreadPool::getInstance()-addFunction([](){ProcessFunc333();});ThreadPool::getInstance()-addFunction([](){ProcessFunc444();});ThreadPool::getInstance()-addFunction([](){ProcessFunc555();});ThreadPool::getInstance()-addFunction([](){ProcessFunc666();});getchar();return 0; }执行结果 tasks begin size:0 stop:0 ThreadPooltasks begin size: 0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 140563906656000 tasks end size:2 ProcessFunc111 begin 140563898263296 tasks end size:4 ProcessFunc222 begin 140563881477888 tasks end size:3 ProcessFunc333 begin 140563743504128 tasks end size:2 ProcessFunc444 begin 140563889870592 tasks end size:1 ProcessFunc555 begin wake up ProcessFunc111 end workerThreadHandler end task tasks begin size:1 stop:0 140563906656000 tasks end size:0 ProcessFunc666 begin ProcessFunc222 end wake up workerThreadHandler end task ProcessFunc333ProcessFunc555ProcessFunc444 endendworkerThreadHandler end taskworkerThreadHandler end taskend workerThreadHandler end task tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 wake up tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 ProcessFunc666 end workerThreadHandler end task tasks begin size:0 stop:0 wake up tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 ...测试程序2,调用线程池停止程序 #include iostream #include chrono #include mutex #include ThreadPool.husing namespace std;static std::mutex m_mutex; void ProcessFunc111() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout__func__ endstd::endl; }void ProcessFunc222() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc333() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc444() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc555() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc666() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout__func__ endstd::endl; }int main() {ThreadPool::getInstance(5);ThreadPool::getInstance()-addFunction([](){ProcessFunc111();});ThreadPool::getInstance()-addFunction([](){ProcessFunc222();});ThreadPool::getInstance()-addFunction([](){ProcessFunc333();});ThreadPool::getInstance()-addFunction([](){ProcessFunc444();});ThreadPool::getInstance()-addFunction([](){ProcessFunc555();});ThreadPool::getInstance()-addFunction([](){ProcessFunc666();});std::this_thread::sleep_for(std::chrono::seconds(1));std::coutstop all std::endl;ThreadPool::getInstance()-stopAll(false);getchar();return 0; }执行结果 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:ThreadPool0 stop:0tasks begin size:0 stop:0 140190941017856 tasks end size:5 ProcessFunc111 begin 140190932625152 tasks end size:4 ProcessFunc222 begin 140190966195968 tasks end size:3 ProcessFunc333 begin 140190949410560 tasks end size:2 ProcessFunc444 begin 140190957803264 tasks end size:1 ProcessFunc555 begin stop all wake up ProcessFunc111 end workerThreadHandler end task ProcessFunc444 end workerThreadHandler end task ProcessFunc333 end workerThreadHandler end task ProcessFunc222 end workerThreadHandler end task ProcessFunc555 end workerThreadHandler end task测试程序3立即停止线程池 #include iostream #include chrono #include mutex #include ThreadPool.husing namespace std;static std::mutex m_mutex; void ProcessFunc111() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout__func__ endstd::endl; }void ProcessFunc222() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc333() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc444() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc555() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout__func__ endstd::endl; }void ProcessFunc666() {std::cout__func__ beginstd::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout__func__ endstd::endl; }int main() {ThreadPool::getInstance(5);ThreadPool::getInstance()-addFunction([](){ProcessFunc111();});ThreadPool::getInstance()-addFunction([](){ProcessFunc222();});ThreadPool::getInstance()-addFunction([](){ProcessFunc333();});ThreadPool::getInstance()-addFunction([](){ProcessFunc444();});ThreadPool::getInstance()-addFunction([](){ProcessFunc555();});ThreadPool::getInstance()-addFunction([](){ProcessFunc666();});std::this_thread::sleep_for(std::chrono::seconds(1));std::coutstop all std::endl;ThreadPool::getInstance()-stopAll(true);getchar();return 0; }执行结果 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 tasks begin size:0 stop:0 ThreadPool 139831215929088 tasks end size:5 ProcessFunc111 begin tasks begin size:5 stop:0 139831199143680 tasks end size:4 ProcessFunc222 begin 139831224321792 tasks end size:3 ProcessFunc333 begin 139831207536384 tasks end size:2 ProcessFunc444 begin 139831232714496 tasks end size:1 ProcessFunc555 begin stop all
http://www.hkea.cn/news/14526968/

相关文章:

  • wordpress站群代开发公司英文
  • 网站建设三网合一指的是什么意思做百度网站还是安居客网站
  • 网站建设规划书样板前端后端都是网站开发吧
  • 张家口专业做网站公司国内公司网站需要备案吗
  • 企业建设网站需注意哪些事项深圳建网站服务商
  • 推荐电商网站建设网站定位模板
  • 手机网站js触屏滑动图片特效给网站如何做飘窗
  • 外包做网站怎么拿源代码平顶山建站公司
  • wamp 网站开发首先做什么修水县城乡建设局网站
  • 四川省建设工程设备安全协会网站济南城市建设职业学院官网招生网
  • 毕业设计代做哪个网站好写作网站推荐
  • 电子商务个人网站可以备案吗翻书效果网站
  • 电子商务网站建设的参考文献电商眼
  • 临沂酒店建设信息网站如何做百度推广
  • 网站建设有哪些家wordpress付费下载模板
  • 做移门的网站wordpress为什么打开商城非常慢
  • 有域名后续怎么做网站织梦网站内容管理系统
  • 网站建设优化排名推广做的网站
  • 安阳网站建设首选外贸企业网站模版
  • 网站广告位价格一般多少wordpress留言板代码
  • 企业建设网站的重要性下载站用什么网站系统
  • 网站内怎样做关键词有效果网站目录命名规则
  • 网站团队的建设网站 上一篇 下一篇
  • 海西高端网站建设公司室内设计方案ppt案例
  • 建设银行梅李分行网站汉中seo培训
  • 做网站容易吧东莞哪家公司做网站好
  • 网站主机空间企业网站开发文档
  • 网站设计实验报告内容与步骤用.net做购物网站
  • 建设网站有什么好处昆明市住房和城乡建设局网站上看的
  • django 做网站的代码好用的ppt模板网站