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

下载网站php源码辽宁城乡住房建设厅网站首页

下载网站php源码,辽宁城乡住房建设厅网站首页,添加网站描述,微网站开发教程一、线程池设计 概念:线程池是一种管理线程资源的机制,预先创建一定数量的线程,当有任务提交时,从线程池中获取线程执行任务,任务完成后线程不会销毁而是返回线程池等待下一个任务。核心组件 线程管理器:负…

一、线程池设计

  • 概念:线程池是一种管理线程资源的机制,预先创建一定数量的线程,当有任务提交时,从线程池中获取线程执行任务,任务完成后线程不会销毁而是返回线程池等待下一个任务。
  • 核心组件
    • 线程管理器:负责创建、销毁和管理线程。
    • 任务队列:存储待执行的任务。
    • 任务接口:定义任务的执行方式。
  • C++实现示例
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>class ThreadPool {
public:ThreadPool(size_t threads);template<class F, class... Args>auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type>;~ThreadPool();
private:// 工作线程集合std::vector<std::thread> workers;// 任务队列std::queue<std::function<void()>> tasks;// 同步相关std::mutex queue_mutex;std::condition_variable condition;bool stop;
};// 构造函数,创建指定数量的工作线程
inline ThreadPool::ThreadPool(size_t threads): stop(false) {for(size_t i = 0; i < 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, class... Args>
auto ThreadPool::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 ThreadPool");tasks.emplace([task]() { (*task)(); });}condition.notify_one();return res;
}// 析构函数,销毁线程池
inline ThreadPool::~ThreadPool() {{std::unique_lock<std::mutex> lock(queue_mutex);stop = true;}condition.notify_all();for(std::thread &worker : workers) {worker.join();}
}
  • 使用示例
// 创建一个包含4个线程的线程池
ThreadPool pool(4);// 向线程池提交任务
std::vector<std::future<int>> results;for(int i = 0; i < 8; ++i) {results.emplace_back(pool.enqueue([i] {std::cout << "hello " << i << std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));std::cout << "world " << i << std::endl;return i*i;}));
}// 等待所有任务完成并获取结果
for(auto && result : results)std::cout << result.get() << ' ';
std::cout << std::endl;

二、锁优化

读写锁
  • 概念:读写锁允许多个线程同时读取共享资源,但在写操作时会独占资源,实现“多读单写”的机制。
  • C++实现
#include <shared_mutex>  // C++17引入
#include <mutex>
#include <map>
#include <string>class ThreadSafeHashMap {
private:mutable std::shared_mutex mtx;std::map<std::string, int> data;public:// 读操作,允许多个线程同时进行int get(const std::string& key) const {std::shared_lock<std::shared_mutex> lock(mtx);auto it = data.find(key);return it != data.end() ? it->second : -1;}// 写操作,独占锁void set(const std::string& key, int value) {std::unique_lock<std::shared_mutex> lock(mtx);data[key] = value;}
};
  • 适用场景:适用于读多写少的场景,如缓存系统。
无锁队列
  • 概念:无锁队列使用原子操作替代传统的锁机制,避免线程阻塞,提高并发性能。
  • C++实现(基于CAS操作)
#include <atomic>
#include <memory>template<typename T>
class LockFreeQueue {
private:struct Node {T data;std::atomic<Node*> next;Node(const T& value) : data(value), next(nullptr) {}};std::atomic<Node*> head;std::atomic<Node*> tail;public:LockFreeQueue() : head(nullptr), tail(nullptr) {}void enqueue(const T& value) {Node* newNode = new Node(value);Node* oldTail = tail.load();while (!tail.compare_exchange_weak(oldTail, newNode)) {}if (oldTail) {oldTail->next = newNode;} else {head = newNode;}}bool dequeue(T& value) {Node* oldHead = head.load();while (oldHead) {Node* next = oldHead->next.load();if (head.compare_exchange_weak(oldHead, next)) {value = oldHead->data;delete oldHead;return true;}}return false;}~LockFreeQueue() {T value;while (dequeue(value)) {}}
};
  • 适用场景:适用于高性能要求的生产者-消费者模型。

性能对比与选择策略

技术优点缺点适用场景
传统互斥锁实现简单,适用广泛线程阻塞,上下文切换开销大竞争不激烈的场景
读写锁提高读并发性能实现复杂,写操作可能饥饿读多写少的场景
无锁队列无阻塞,高性能实现复杂,调试困难高性能要求的队列操作

在实际应用中,需要根据具体场景选择合适的并发技术,必要时可以组合使用多种技术以达到最佳性能。


文章转载自:

http://RMWbL9OO.fgLzk.cn
http://TIAGqeyr.fgLzk.cn
http://L3TNuXux.fgLzk.cn
http://SMuVuZiu.fgLzk.cn
http://ZRseQeMO.fgLzk.cn
http://w4Qm9m4g.fgLzk.cn
http://t4t6483w.fgLzk.cn
http://9Vrpk3fj.fgLzk.cn
http://r4qEvCY3.fgLzk.cn
http://hkLAgSzL.fgLzk.cn
http://RrDufUkb.fgLzk.cn
http://bCokifVu.fgLzk.cn
http://e6Z7UfOR.fgLzk.cn
http://lDsDeHUO.fgLzk.cn
http://7jD4Yrn3.fgLzk.cn
http://SY1WQcV2.fgLzk.cn
http://cSMVECyr.fgLzk.cn
http://o6z3DWnJ.fgLzk.cn
http://2PZIX49D.fgLzk.cn
http://lSrYKAdX.fgLzk.cn
http://CYHMvl9B.fgLzk.cn
http://lwXw0m2x.fgLzk.cn
http://pA7cuWHZ.fgLzk.cn
http://I4r9JZcy.fgLzk.cn
http://tlYZ3BR9.fgLzk.cn
http://XV5w9Rxn.fgLzk.cn
http://HlprRWeZ.fgLzk.cn
http://SWWpjAp6.fgLzk.cn
http://PZrQJgoT.fgLzk.cn
http://YttNpvyO.fgLzk.cn
http://www.dtcms.com/wzjs/667980.html

相关文章:

  • 芜湖学校网站建设电话举例说明网络营销的概念
  • 微网站开发的比较总结海南最新消息新闻
  • 建设一个网站价格wordpress店铺模板制作教程
  • 贵阳网站建设建站解决方案手机网站怎么导入微信
  • 东营网站建设app开发备案 新增网站
  • 做什么软件做网站效率最好软件公司需要什么资质
  • 网站建设需要多少天时间app软件开发工具包
  • 网站案例代码网站设计网
  • 建设仿优酷视频网站如何做品牌网站设计
  • 公司简介模板怎么做seo优化方案策划书
  • thinkcmf 做企业网站wordpress扩容
  • 做媛网站wordpress 新手
  • 电脑做服务器上传网站加强门户网站建设
  • 乌镇网站开发文档大男人直播视频
  • 公司网站简介网页剪辑app
  • 个人网站主页郑州专业手机网站制作
  • 就业服务网站建设方案个人app开发平台免费
  • 网站建设 电话wordpress is_login
  • 城建公司建设网站基础资料深圳华强北在哪
  • 建设网站企业网上银行登录入口官方运城网站推广哪家好
  • 上海网站建设 网络推广阿里指数查询官网入口
  • 专业网站制作公司采用哪些技术制作网站?网上建立网站
  • 做企业网站 签合同要注意什么想开广告公司怎么起步
  • 12306网站为什么做不好使建设网站怎样赚钱
  • 做外汇看哪个网站五莲县城乡建设局网站首页
  • 左侧导航栏网站广州效果图设计公司
  • 宜兴市建设局网站四川建设银行官网招聘网站
  • 网站建设上市廊坊seo整站优化软件
  • 芜湖网站开发商业街网站建设方案
  • 为什么用wp做网站济南电子商务网站建设