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

asp网站服务器架设峨山网站建设

asp网站服务器架设,峨山网站建设,在阿里云里网站建设的步骤过程,晋江论坛匿名区目录 一、线程池简介线程池的核心组件实现步骤 二、C11实现线程池源码 三、线程池源码解析1. 成员变量2. 构造函数2.1 线程初始化2.2 工作线程逻辑 3. 任务提交(enqueue方法)3.1 方法签名3.2 任务封装3.3 任务入队 4. 析构函数4.1 停机控制 5. 关键技术点解析5.1 完美转发实现5… 目录 一、线程池简介线程池的核心组件实现步骤 二、C11实现线程池源码 三、线程池源码解析1. 成员变量2. 构造函数2.1 线程初始化2.2 工作线程逻辑 3. 任务提交(enqueue方法)3.1 方法签名3.2 任务封装3.3 任务入队 4. 析构函数4.1 停机控制 5. 关键技术点解析5.1 完美转发实现5.2 异常传播机制5.3 内存管理模型 四、 性能特征分析五、 扩展优化方向六、 典型问题排查指南七、 测试用例如果这篇文章对你有所帮助渴望获得你的一个点赞 一、线程池简介 线程池是一种并发编程技术通过预先创建一组线程并复用它们来执行多个任务避免了频繁创建和销毁线程的开销。它特别适合处理大量短生命周期任务的场景如服务器请求、并行计算。 线程池的核心组件 1. 任务队列Task Queue 存储待执行的任务通常是函数对象或可调用对象。 2. 工作线程Worker Threads 一组预先创建的线程不断从队列中取出任务并执行。 3. 同步机制 互斥锁Mutex保护任务队列的线程安全访问。 条件变量Condition Variable通知线程任务到达或线程池终止。 实现步骤 1. 初始化线程池 创建固定数量的线程每个线程循环等待任务。 2. 提交任务 将任务包装成函数对象加入任务队列。 3. 任务执行 工作线程从队列中取出任务并执行。 4. 终止线程池 发送停止信号等待所有线程完成当前任务后退出。 二、C11实现线程池 源码 #include vector #include queue #include future #include thread #include mutex #include condition_variable #include functional #include stdexceptclass ThreadPool { public://构造函数根据输入的线程数默认硬件并发数创建工作线程。//每个工作线程执行一个循环不断从任务队列中取出并执行任务。//explicit关键字防止隐式类型转换explicit ThreadPool(size_t threads std::thread::hardware_concurrency()): stop(false) {if (threads 0) {threads 1;}for (size_t i 0; i threads; i) {workers.emplace_back([this] {for (;;) {std::functionvoid() task;{std::unique_lockstd::mutex lock(this-queue_mutex);//等待条件线程通过条件变量等待任务到来或停止信号。(CPU使用率休眠时接近0%仅在任务到来时唤醒)//lambda表达式作为谓词当条件(停止信号为true 或 任务队列非空)为真时才会解除阻塞。this-condition.wait(lock, [this] {return (this-stop || !this-tasks.empty());});/* 传统忙等待while (!(stop || !tasks.empty())) {} // 空循环消耗CPU */if (this-stop this-tasks.empty()){//如果线程池需要终止且任务队列为空则直接returnreturn;}//任务提取从队列中取出任务并执行使用std::move避免拷贝开销。task std::move(this-tasks.front());this-tasks.pop();}//执行任务task();}});}}//任务提交enqueue方法templateclass F, class... Argsauto enqueue(F f, Args... args)- std::futuretypename std::result_ofF(Args...)::type {using return_type typename std::result_ofF(Args...)::type;//任务封装使用std::packaged_task包装用户任务支持异步返回结果。//智能指针管理shared_ptr确保任务对象的生命周期延续至执行完毕。//完美转发通过std::forward保持参数的左值/右值特性。auto task std::make_sharedstd::packaged_taskreturn_type()(std::bind(std::forwardF(f), std::forwardArgs(args)...));std::futurereturn_type res task-get_future();{std::unique_lockstd::mutex lock(queue_mutex);if (stop){throw std::runtime_error(enqueue on stopped ThreadPool);} tasks.emplace([task]() { (*task)(); });/* push传入的对象需要事先构造好再复制过去插入容器中而emplace则可以自己使用构造函数所需的参数构造出对象并直接插入容器中。emplace相比于push省去了复制的步骤则使用emplace会更加节省内存。*/}condition.notify_one();return res;}~ThreadPool() {//设置stop标志唤醒所有线程等待任务队列清空。{std::unique_lockstd::mutex lock(queue_mutex);stop true;}condition.notify_all();for (std::thread worker : workers){worker.join();}}private:std::vectorstd::thread workers; //存储工作线程对象std::queuestd::functionvoid() tasks; //任务队列存储待执行的任务std::mutex queue_mutex; //保护任务队列的互斥锁std::condition_variable condition; //线程间同步的条件变量bool stop; //线程池是否停止标志 };三、线程池源码解析 1. 成员变量 std::vectorstd::thread workers; // 工作线程容器 std::queuestd::functionvoid() tasks; // 任务队列 std::mutex queue_mutex; // 队列互斥锁 std::condition_variable condition; // 条件变量 bool stop; // 停机标志设计要点: 采用生产者-消费者模式任务队列作为共享资源 组合使用mutexcondition_variable实现线程同步 vector存储线程对象便于统一管理生命周期 2. 构造函数 2.1 线程初始化 explicit ThreadPool(size_t threads std::thread::hardware_concurrency()): stop(false) {if (threads 0) {threads 1;}for (size_t i 0; i threads; i) {workers.emplace_back([this] { /* 工作线程逻辑 */ });} }设计要点: explicit防止隐式类型转换如ThreadPool pool 4; 默认使用硬件并发线程数通过hardware_concurrency() 最少创建1个线程避免空池 使用emplace_back直接构造线程对象 2.2 工作线程逻辑 for (;;) {std::functionvoid() task;{std::unique_lockstd::mutex lock(queue_mutex);condition.wait(lock, [this] {return stop || !tasks.empty();});if (stop tasks.empty()) {return; }task std::move(tasks.front());tasks.pop();}task(); }核心机制: unique_lock配合条件变量实现自动锁管理 双重状态检查停机标志队列非空 任务提取使用移动语义避免拷贝 任务执行在锁作用域外进行 3. 任务提交(enqueue方法) 3.1 方法签名 templateclass F, class... Args auto enqueue(F f, Args... args)- std::futuretypename std::result_ofF(Args...)::type类型推导: 使用尾置返回类型声明std::result_of推导可调用对象的返回类型完美转发参数FArgs... 3.2 任务封装 auto task std::make_sharedstd::packaged_taskreturn_type()(std::bind(std::forwardF(f), std::forwardArgs(args)...));封装策略: packaged_task包装任务用于异步获取结果shared_ptr管理任务对象生命周期std::bind绑定参数注意C11的参数转发限制 3.3 任务入队 tasks.emplace([task]() { (*task)(); });优化点: 使用emplace直接构造队列元素Lambda捕获shared_ptr保持任务有效性显式解引用执行packaged_task 4. 析构函数 4.1 停机控制 ~ThreadPool() {{std::unique_lockstd::mutex lock(queue_mutex);stop true;}condition.notify_all();for (auto worker : workers){worker.join();} }停机协议: 设置停机标志原子操作广播唤醒所有等待线程等待所有工作线程退出 5. 关键技术点解析 5.1 完美转发实现 std::bind(std::forwardF(f), std::forwardArgs(args)...)保持参数的左右值特性支持移动语义参数的传递C11的限制无法完美转发所有参数类型 5.2 异常传播机制 任务异常通过future对象传播packaged_task自动捕获异常用户通过future.get()获取异常 5.3 内存管理模型 [任务提交者]|v[packaged_task] ---- shared_ptr ---- [任务队列]|v[future]三重生命周期保障 提交者持有future队列持有任务包装器工作线程执行任务 四、 性能特征分析 1. 时间复杂度 操作时间复杂度任务提交(enqueue)O(1)加锁开销任务提取O(1)线程唤醒取决于系统调度 2. 空间复杂度 组件空间占用线程栈每线程MB级任务队列与任务数成正比同步原语固定大小 五、 扩展优化方向 1. 任务窃取Work Stealing 实现多个任务队列空闲线程从其他队列窃取任务 2. 动态线程池 void adjust_workers(size_t new_size) {if (new_size workers.size()) {// 扩容逻辑} else {// 缩容逻辑} }3. 优先级队列 using Task std::pairint, std::functionvoid(); // 优先级任务std::priority_queueTask tasks;4. 无锁队列 moodycamel::ConcurrentQueuestd::functionvoid() tasks;六、 典型问题排查指南 现象可能原因解决方案任务未执行线程池提前析构延长线程池生命周期future.get()永久阻塞任务未提交/异常未处理检查任务提交路径CPU利用率100%忙等待或锁竞争优化任务粒度/使用无锁结构内存持续增长任务对象未正确释放检查智能指针使用 该实现完整展现了现代C线程池的核心设计范式开发者可根据具体需求在此基础进行功能扩展和性能优化。理解这个代码结构是掌握更高级并发模式的基础。 七、 测试用例 使用实例C11兼容 #include iostreamint main() {ThreadPool pool(4);// 提交普通函数auto future1 pool.enqueue([](int a, int b) {return a b;}, 2, 3);// 提交成员函数struct Calculator {int multiply(int a, int b) { return a * b; }} calc;auto future2 pool.enqueue(std::bind(Calculator::multiply, calc, std::placeholders::_1, std::placeholders::_2), 4, 5);// 异常处理示例auto future3 pool.enqueue([]() - int {throw std::runtime_error(example error);return 1;});std::cout 23 future1.get() std::endl;std::cout 4*5 future2.get() std::endl;try {future3.get();} catch(const std::exception e){std::cout Caught exception: e.what() std::endl;}return 0; }如果这篇文章对你有所帮助渴望获得你的一个点赞
文章转载自:
http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn
http://www.morning.dgng.cn.gov.cn.dgng.cn
http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn
http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn
http://www.morning.lywpd.cn.gov.cn.lywpd.cn
http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn
http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn
http://www.morning.oioini.com.gov.cn.oioini.com
http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn
http://www.morning.zfyr.cn.gov.cn.zfyr.cn
http://www.morning.smpmn.cn.gov.cn.smpmn.cn
http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn
http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn
http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn
http://www.morning.yhywx.cn.gov.cn.yhywx.cn
http://www.morning.litao4.cn.gov.cn.litao4.cn
http://www.morning.rwqj.cn.gov.cn.rwqj.cn
http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn
http://www.morning.sgqw.cn.gov.cn.sgqw.cn
http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn
http://www.morning.ejknty.cn.gov.cn.ejknty.cn
http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn
http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn
http://www.morning.amonr.com.gov.cn.amonr.com
http://www.morning.kyctc.cn.gov.cn.kyctc.cn
http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn
http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn
http://www.morning.mjctt.cn.gov.cn.mjctt.cn
http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn
http://www.morning.kgsws.cn.gov.cn.kgsws.cn
http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn
http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn
http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn
http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn
http://www.morning.nbybb.cn.gov.cn.nbybb.cn
http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn
http://www.morning.kflbf.cn.gov.cn.kflbf.cn
http://www.morning.cfnht.cn.gov.cn.cfnht.cn
http://www.morning.tkztx.cn.gov.cn.tkztx.cn
http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn
http://www.morning.mytmx.cn.gov.cn.mytmx.cn
http://www.morning.snktp.cn.gov.cn.snktp.cn
http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn
http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn
http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn
http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn
http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn
http://www.morning.sdamsm.com.gov.cn.sdamsm.com
http://www.morning.qysnd.cn.gov.cn.qysnd.cn
http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn
http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn
http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn
http://www.morning.ksggr.cn.gov.cn.ksggr.cn
http://www.morning.whpsl.cn.gov.cn.whpsl.cn
http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn
http://www.morning.wklmj.cn.gov.cn.wklmj.cn
http://www.morning.tkztx.cn.gov.cn.tkztx.cn
http://www.morning.ywxln.cn.gov.cn.ywxln.cn
http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn
http://www.morning.lpnb.cn.gov.cn.lpnb.cn
http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn
http://www.morning.kybyf.cn.gov.cn.kybyf.cn
http://www.morning.ylpl.cn.gov.cn.ylpl.cn
http://www.morning.srwny.cn.gov.cn.srwny.cn
http://www.morning.nypgb.cn.gov.cn.nypgb.cn
http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn
http://www.morning.lmqw.cn.gov.cn.lmqw.cn
http://www.morning.leyuhh.com.gov.cn.leyuhh.com
http://www.morning.snbq.cn.gov.cn.snbq.cn
http://www.morning.c7510.cn.gov.cn.c7510.cn
http://www.morning.yrbq.cn.gov.cn.yrbq.cn
http://www.morning.xqjz.cn.gov.cn.xqjz.cn
http://www.morning.pntzg.cn.gov.cn.pntzg.cn
http://www.morning.bgkk.cn.gov.cn.bgkk.cn
http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn
http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn
http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn
http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn
http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn
http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn
http://www.tj-hxxt.cn/news/238379.html

相关文章:

  • 网站菜单导航制作创艺装饰公司官网
  • 监测网站定制网站建设大
  • 郑州网站商城建设wordpress显示大图
  • 山东省建设局网站监理员考试同一虚拟主机 2个网站
  • 企业网站开发要学什么tv域名的网站
  • 广西网站建设哪家好龙之向导外贸专区
  • 广州网站制作方法网站seo完整的优化方案
  • 网站建设公司的发展前景网站建设 工具
  • 安徽鸿顺鑫城建设集团网站wordpress仿堆糖网
  • 深圳市光明区住房和建设局网站商标注册网上缴费流程
  • 邢台做网站哪儿好城乡建设吧部网站
  • 番禺制作网站系统wordpress下载页面天涯
  • 公司要建个网站建设个人技术网站
  • 网站服务公司名称wordpress 网站收录
  • 高端互联网网站网站源码传到服务器上后怎么做
  • 网站策划中规划预测怎们做windows优化大师怎么使用
  • 建设校园网站国外研究现状甘肃省建设厅官方网站
  • 深圳营销型网站建网站建设进度表
  • 软件开发类论文基本结构seo是什么姓氏
  • 无忧中英繁企业网站系统 完整附近哪里有建设
  • 公司招聘网站有哪些太原站还建综合楼
  • 做网站用什么写怎样申请自媒体账号
  • 扁平化设计风格的网站深圳专业网站建设公司
  • 365网站建设网站怎样制作
  • 英文网站建设成都适合当手机主页的网站
  • 建立问答类的网站ps制作网站logo
  • 加强普法网站和普法网络集群建设专业做租赁的平台网站有哪些
  • 58这样网站怎么做平潭建设局网站首页
  • 网站 防止采集手机网站推荐一个
  • 山西建设网站企业公司做网页去哪找