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

深圳市官网网站建设报价沧浪seo网站优化软件

深圳市官网网站建设报价,沧浪seo网站优化软件,wordpress填错地址,卡盟怎么做网站目录 一、问题引出 二、优化建议与改进方案 1. 基础加固 2. 功能增强 3. 性能优化 三、完整优化代码示例 四、关键优化点总结 1.安全性增强: 2.功能扩展: 3.性能优化: 4.工业级特性: 五、测试验证建议 一、问题引出 …

目录

一、问题引出

二、优化建议与改进方案

1. 基础加固

2. 功能增强

3. 性能优化

三、完整优化代码示例

四、关键优化点总结

1.安全性增强:

2.功能扩展:

3.性能优化:

4.工业级特性:

五、测试验证建议


一、问题引出

在阿Q技术站看到一个c++线程池的实现。该线程池还有可以改进的地方。

#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iostream>class ThreadPool{
public:ThreadPool(size_t num_threads) : stop(false) {for(size_t i = 0; i < num_threads; ++i) {workers.emplace_back([this] {while(true) {std::function<void()> task;{std::unique_lock<std::mutex> lock(this->queue_mutex);this->condition.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();}});}}template<class F>void enqueue(F&& f) {{std::unique_lock<std::mutex> lock(queue_mutex);tasks.emplace(std::forward<F>(f));}condition.notify_one();}~ThreadPool() {{std::unique_lock<std::mutex> lock(queue_mutex);stop = true;}condition.notify_all();for(std::thread &worker : workers)worker.join();}private:std::vector<std::thread> workers;std::queue<std::function<void()>> tasks;std::mutex queue_mutex;std::condition_variable condition;bool stop;
};int main() {ThreadPool pool(4); // 创建4个线程的线程池for (int i = 0; i < 8; ++i) {pool.enqueue([i]() {std::cout << "Task " << i << " executed by thread " << std::this_thread::get_id() << "\n";std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 模拟工作});}std::this_thread::sleep_for(std::chrono::seconds(1)); // 等待任务完成return 0;
}

逻辑问题:

  1. 在enqueue时,如果线程池已经停止(stop=true),仍然可以添加任务,这可能导致在析构后执行任务,引发未定义行为。需要在enqueue时检查stop标志。
  2. 异常处理:如果task()抛出异常,线程将终止,导致线程池中的线程数减少,影响后续任务执行。
  3. stop标志可以改用原子类型。

二、优化建议与改进方案

1. 基础加固

问题修复代码:

// 修改 enqueue 函数
template<class F>
bool enqueue(F&& f) {  // 返回是否提交成功{std::unique_lock<std::mutex> lock(queue_mutex);if(stop) return false;  // 池已关闭拒绝任务tasks.emplace(std::forward<F>(f));}condition.notify_one();return true;
}

2. 功能增强

(1) 支持任务返回值

template<class F>
auto enqueue(F&& f) -> std::future<decltype(f())> {using RetType = decltype(f());auto task = std::make_shared<std::packaged_task<RetType()>>(std::forward<F>(f));std::future<RetType> res = task->get_future();{std::unique_lock<std::mutex> lock(queue_mutex);if(stop) throw std::runtime_error("enqueue on stopped pool");tasks.emplace([task]() { (*task)(); });}condition.notify_one();return res;
}

(2) 异常安全处理

// 修改工作线程循环
try {task();
} catch (...) {// 记录异常或传递异常std::lock_guard<std::mutex> lock(exception_mutex);exceptions.push_back(std::current_exception());
}

3. 性能优化

(1) 无锁队列替换

#include <boost/lockfree/queue.hpp> // 或实现无锁队列class ThreadPool {
private:boost::lockfree::queue<std::function<void()>> tasks{1000}; // 初始化容量// 移除 mutex 和 condition_variable
};

(2) 动态线程调整

void resize(size_t new_size) {if (new_size < workers.size()) {// 减少线程} else if (new_size > workers.size()) {// 增加线程}
}

三、完整优化代码示例

#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <stdexcept>
#include <functional>class ThreadPool {
public:explicit ThreadPool(size_t threads, size_t max_tasks = 1000) : stop(false), task_count(0) {if(threads == 0) threads = std::thread::hardware_concurrency();workers.reserve(threads);for(size_t i = 0; i < threads; ++i) {workers.emplace_back([this] {while(true) {std::function<void()> task;{std::unique_lock<std::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_count;}try {task();} catch (...) {std::lock_guard<std::mutex> elock(exception_mutex);exceptions.push_back(std::current_exception());}}});}}template<class F, class... Args>auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> {using return_type = typename std::result_of<F(Args...)>::type;auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));std::future<return_type> res = task->get_future();{std::unique_lock<std::mutex> lock(queue_mutex);if(stop) throw std::runtime_error("enqueue on stopped pool");tasks.emplace([task](){ (*task)(); });++task_count;}condition.notify_one();return res;}~ThreadPool() {{std::unique_lock<std::mutex> lock(queue_mutex);stop = true;}condition.notify_all();for(std::thread &worker : workers)if(worker.joinable()) worker.join();if(!exceptions.empty()) {// 异常处理逻辑}}private:std::vector<std::thread> workers;std::queue<std::function<void()>> tasks;std::mutex queue_mutex;std::condition_variable condition;std::atomic_bool stop;std::atomic<size_t> task_count;std::mutex exception_mutex;std::vector<std::exception_ptr> exceptions;
};

四、关键优化点总结

1.安全性增强

  • 原子操作 stop 和 task_count

  • 异常捕获与传递机制

  • 入队前状态检查

2.功能扩展

  • 支持任务返回值(future/packaged_task

  • 动态线程数量调整预留接口

  • 任务上限保护(通过 max_tasks 参数)

3.性能优化

  • 原子计数器减少锁竞争

  • 任务批处理优化(示例未展示,可添加)

  • 无锁队列选项(需第三方库支持)

4.工业级特性

  • 硬件并发数自动检测

  • 资源释放保障(joinable() 检查)

  • 防止任务堆积的拒绝策略


五、测试验证建议

#include<cassert>
/*
前面的线程池代码放在这
*/// 验证用例
void test_pool() {ThreadPool pool(4);auto future = pool.enqueue([](int a) { return a*a; }, 5);assert(future.get() == 25);// 压力测试for(int i=0; i<10000; ++i) {pool.enqueue([i]{ std::this_thread::sleep_for(std::chrono::microseconds(1)); });}
}int main(int argc,char* argv[])
{test_pool();return 0;
}

通过静态分析工具(如Clang-Tidy)和动态测试(如ThreadSanitizer)确保线程安全。

http://www.dtcms.com/wzjs/130174.html

相关文章:

  • office做网站的软件电商代运营十大公司排名
  • 网页开发人员工具厦门seo关键词优化
  • 网站建设价格请咨询兴田德润seo长沙
  • 公司自建网站备案电商平台运营方案
  • 西安未央区网站建设seo搜索优化
  • 中国农业建设网站百度一下你就知道原版
  • wordpress主题汉化是什么宁波seo教程行业推广
  • 网站域名查主机百度广告价格
  • 做微网站需要域名吗什么是网站seo
  • wordpress 中文注册成都自然排名优化
  • 免费制作企业贺卡seo推广代运营
  • 新增备案 网站名字百度app官网下载安装
  • 免费静态网站模板下载软文营销怎么写
  • 找人做设计的网站厦门人才网
  • 婚庆公司网站建设方案百度广告公司
  • 嘉兴建设局网站淘宝关键词优化软件
  • 有什么网站可以做微信支付宝支付宝长尾关键词什么意思
  • 网站制作知识刚刚中国宣布重大消息
  • wordpress和pageadmin湖南网站seo
  • 做齐鲁油官方网站网站seo搜索引擎优化教程
  • 深圳东门疫情信息流优化师简历
  • 南宁网站建设云尚网络淘宝关键词怎么做排名靠前
  • 网上共青团智慧团建官网登录网址seo优化方法网站快速排名推广渠道
  • 关于建设网站的会议纪要代运营一个月多少钱
  • 用php做的网站怎么上传营销策略理论
  • 建网站选哪个网络营销企业网站
  • java是做网站的吗作品推广
  • 网页设计制作音乐网站网易最新消息新闻
  • 专题探索网站开发模式特点广告文案经典范例200字
  • 百度搜索不到自己的网站移动排名提升软件