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

企业网站源码交易网页升级访问中每天正常

企业网站源码交易,网页升级访问中每天正常,wordpress链接在哪里设置密码,展厅展台设计搭建原理说明: 1. 线程池创建时,指定线程池的大小thread_size。当有新的函数任务通过函数addFunction ()添加进来后,其中一个线程执行函数。一个线程一次执行一个函数。如果函数数量大与线程池数量,则后来的函数等待。 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 <vector>class ThreadPool {
public:static ThreadPool* getInstance(size_t thread_size = 1);		//默认线程池大小void addFunction(std::function<void()> task);				//添加需要执行的函数~ThreadPool();void stopAll(bool immediately);						//停止线程池, immediately:true立即停止, immediately:false等待当前线程函数执行完后停止。
private:ThreadPool(size_t thread_size);private:void workerThreadHandler();std::vector<std::thread> m_workers;					//线程容器std::queue<std::function<void()>> 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_ptr<ThreadPool> m_instance=nullptr;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 i=0; i<thread_size; ++i){m_workers.emplace_back([this, i](){	//创建线程池中的线程workerThreadHandler();});}//辅助线程,每隔一段时间发送一次唤醒,防止线程阻塞m_wakeTimerThread = std::thread([this](){	for(;!this->m_stop;){std::unique_lock<std::mutex> lock(this->m_timer_mutex);std::this_thread::sleep_for(std::chrono::milliseconds(2000));pthread_testcancel();m_condition.notify_all();std::cout<<"wake up"<<std::endl;}});std::cout<<__func__<<std::endl;
}//线程循环函数,循环查询函数容器是否为空,不为空则读取一个函数并执行。
void ThreadPool::workerThreadHandler()	
{for (;!this->m_stop;){std::function<void()> task;{std::unique_lock<std::mutex> lock(this->m_queue_mutex);std::cout<<"tasks begin size:"<<this->m_functions.size()<<" stop:"<<m_stop<<std::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::cout<<std::this_thread::get_id() <<" tasks end size:"<<this->m_functions.size()<<std::endl;}task();std::cout<<__func__<<" end task"<<std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(200));}
}void ThreadPool::addFunction(std::function<void()> task)	//添加待执行的函数
{std::unique_lock<std::mutex> lock(m_queue_mutex);if (m_stop){return;}m_functions.emplace(std::move(task));m_condition.notify_one();
}ThreadPool::~ThreadPool()
{{std::unique_lock<std::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.h"using namespace std;static std::mutex m_mutex;
void ProcessFunc111()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc222()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc333()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc444()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc555()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc666()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout<<__func__<<" end"<<std::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.h"using namespace std;static std::mutex m_mutex;
void ProcessFunc111()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc222()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc333()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc444()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc555()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc666()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout<<__func__<<" end"<<std::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::cout<<"stop 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.h"using namespace std;static std::mutex m_mutex;
void ProcessFunc111()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc222()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc333()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc444()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc555()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(4));std::cout<<__func__<<" end"<<std::endl;
}void ProcessFunc666()
{std::cout<<__func__<<" begin"<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(3));std::cout<<__func__<<" end"<<std::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::cout<<"stop 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.yayakq.cn/news/354605/

相关文章:

  • 工程建设监理学校网站旅游营销型网站
  • 一个考试网站怎么做会计信息系统网站建设流程图
  • 深圳网站建设公司专业赣州信息港
  • 海外学校网站建设净化科技网站建设
  • 网站开发需求分析说明免费的cms模板
  • 青浦做网站公司免费文档模板网站
  • 长沙建站网国企网站建设要求
  • 不错的免费网站建设上海制作网页的公司有哪些
  • 电子商务的网站怎么做电子商务职业生涯规划书
  • app网站建设开发网站建设中采用的技术
  • 深圳网站设计推荐柚米网站建设自己怎么做
  • 电器企业网站建设方案书做购物网站赚钱吗
  • 百度营销-网站分析培训wordpress类目
  • 企业网站建设设计公司龙岩网站建设大概费用
  • 简洁大气企业网站欣赏网上银行登录入口
  • 北京专业做网站设计公司建立网站的详细步骤图解
  • 沧州网站建设哪家好2014网站设计趋势
  • 政务网站系统中国化学第九建设公司网站
  • 企业网站建设都能做哪些工作定制型网站设计价格
  • 青岛市两个体系建设网站响应式网站开发技术
  • 贵州小城镇建设网站专业邯郸网站建设
  • 网站备案网站东莞微网站建设费用
  • 企业网站备案时间网站建设合作
  • 哪建设网站昆明seo怎么做
  • 网站建设公司广告语福建省建设工程质量安全网
  • 天河网站建设开发如何查看一个网站是用什么程序做的
  • 网站切换语言怎么做的建设通网站有建筑公司名录大全
  • 建设网站视频素材广州市官网网站建设报价
  • 网站建设的空间指的是网站建设的方案预算
  • 做网站买域名要买几个后缀最安全建设一个公司的网站需要多少钱