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

郴州网站建设公司哪里有企业名录查询

郴州网站建设公司哪里有,企业名录查询,网站怎么做海外推广方案,dw做响应式网站线程池可以有效地管理和重用线程资源&#xff0c;避免频繁创建和销毁线程带来的开销。以下是一个简单的线程池示例。 cpp #include <iostream> #include <vector> #include <thread> #include <queue> #include <mutex> #include <condition…

线程池可以有效地管理和重用线程资源,避免频繁创建和销毁线程带来的开销。以下是一个简单的线程池示例。

cpp
#include <iostream>
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>class ThreadPool {
public:ThreadPool(size_t numThreads);~ThreadPool();void enqueue(std::function<void()> func);private:std::vector<std::thread> workers;std::queue<std::function<void()>> tasks;std::mutex queueMutex;std::condition_variable condition;bool stop;void worker();
};ThreadPool::ThreadPool(size_t numThreads) : stop(false) {for (size_t i = 0; i < numThreads; ++i) {workers.emplace_back([this] { this->worker(); });}
}ThreadPool::~ThreadPool() {{std::unique_lock<std::mutex> lock(queueMutex);stop = true;}condition.notify_all();for (std::thread &worker : workers) {worker.join();}
}void ThreadPool::enqueue(std::function<void()> func) {{std::unique_lock<std::mutex> lock(queueMutex);tasks.push(func);}condition.notify_one();
}void ThreadPool::worker() {while (true) {std::function<void()> task;{std::unique_lock<std::mutex> lock(queueMutex);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();}
}// 示例使用
void exampleTask(int n) {std::cout << "Task " << n << " is being processed by thread " << std::this_thread::get_id() << std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));
}int main() {ThreadPool pool(4); // 创建具有4个线程的线程池for (int i = 0; i < 10; ++i) {pool.enqueue([i] { exampleTask(i); });}`在这里插入代码片`std::this_thread::sleep_for(std::chrono::seconds(5)); // 保证主线程等待足够长的时间让线程池处理完任务return 0;
}
http://www.yayakq.cn/news/749986/

相关文章:

  • 怎么做网站广告位惠州网红
  • 网站关键词的布局用liferay做的网站
  • 只做网站的2008iis7怎么搭建网站
  • 游戏开发公司哪家好优化网站页面
  • 如何网站增加域名东莞网络推广平台
  • 免费建立自己微网站吗包装设计效果图生成器
  • 网站做SEO优化多少钱河南seo网站多少钱
  • 丹阳网站建设如何免费搭建贴吧系统网站
  • 广告网站设计方案太原seo网站管理
  • 建设部资质网站查询画图标网站
  • 小说网站建设源码wordpress页面模板是哪个文件
  • 哪里有帮做微课的网站免费汽车租赁网站模板
  • 百度词条优化谷歌广告优化
  • 怎么做公司网站优化自己怎么1做网站
  • 贵阳做网站方舟网络wordpress php7不兼容
  • 无极网络济南网站优化分析
  • 宣武网站建设wordpress入门使用
  • 网络网站制作技巧wordpress 08影院
  • seo营销型网站推广国外做外贸的网站
  • 假电影网站做注册网站建设合作分成合同
  • 微信h5网站模板下载wordpress设置登陆口
  • 什么APP可以做网站天创网站
  • 学科基地网站建设wordpress 网校主题
  • 网站后台组成最值得购买 wordpress
  • 如何建立一个网站视频教学网页版传奇链接
  • 设计网站开发方案流程图余姚网站建设服务
  • 绍兴网站推广排名别人不能注册我的wordpress站
  • 湖南营销网站建设设计做外贸纱线用什么网站
  • 建设网站方案ppt湖南营销型网站建设 j磐石网络
  • 织梦网站头部到底部去了宿迁网站建设公司排名