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

定制网站设计公司立即优化在哪里

定制网站设计公司,立即优化在哪里,p2p网站如何做推广,批发价格广州网页设计一、线程池设计 概念:线程池是一种管理线程资源的机制,预先创建一定数量的线程,当有任务提交时,从线程池中获取线程执行任务,任务完成后线程不会销毁而是返回线程池等待下一个任务。核心组件 线程管理器:负…

一、线程池设计

  • 概念:线程池是一种管理线程资源的机制,预先创建一定数量的线程,当有任务提交时,从线程池中获取线程执行任务,任务完成后线程不会销毁而是返回线程池等待下一个任务。
  • 核心组件
    • 线程管理器:负责创建、销毁和管理线程。
    • 任务队列:存储待执行的任务。
    • 任务接口:定义任务的执行方式。
  • 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://www.yidumall.com/news/70016.html

相关文章:

  • 企业3合1网站建设公司销售推广方案
  • jsp 企业建站企业营销策划书范文
  • 在网站上做的h5如何发到微信上自己接单的平台
  • 公司宣传册封面图片谷歌seo网站优化
  • 通过模版做网站网络营销的发展现状如何
  • 浙江均泰建设有限公司网站搜索引擎优化的根本目的
  • 网站开发岗位职责及任职要求seo需要掌握哪些技术
  • 电子商务网站后台核心管理seo推广的全称是
  • 做网站威海哪个平台可以免费打广告
  • 做英文网站建设色盲测试图
  • 景观设计案例网站东莞推广
  • 小游戏开发seo公司 杭州
  • java web网站开发教程百度收录怎么查询
  • 网站开发成本评估优化步骤
  • 如何设计一个有趣的网页虞城seo代理地址
  • 做网站一天赚多少钱线上营销策划案例
  • java做网站好吗网络宣传方式
  • wordpress实例配置唐山百度搜索排名优化
  • 网站建设成功案例怎么写中国国家培训网正规吗
  • 广州网站建设出售站长工具app
  • 拓者设计吧效果图江苏短视频seo搜索
  • 用易语言做钓鱼网站产品推广步骤
  • 360网站弹窗推广怎么做的宁波好的seo外包公司
  • 某网站seo诊断分析杭州网站制作排名
  • 昆明做一个公司网站多少费用百度seo课程
  • 阿里巴巴网站是怎么做的如何让产品吸引顾客
  • 海南做网站我想做app推广怎么做
  • 自己建设的网站靠谱吗百度竞价推广代理商
  • 宠物网站模版游戏推广是什么工作
  • 视差滚动网站模板可以免费打广告的网站