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

工具网站有哪些有做义工的相亲网站吗

工具网站有哪些,有做义工的相亲网站吗,电子商城采购平台官网,简单大气网站模板概要 通常线程池是同质的#xff0c;每个线程都可以执行任意的task#xff08;每个线程中的task顺序执行#xff09;#xff0c;如下图所示#xff1a; 但本文所介绍的线程和task之间有绑定关系#xff0c;如A task只能跑在A thread上#xff08;因此称为异构线程池每个线程都可以执行任意的task每个线程中的task顺序执行如下图所示 但本文所介绍的线程和task之间有绑定关系如A task只能跑在A thread上因此称为异构线程池每个线程的功能是有所区别的如下图所示 接口设计 TThreadPool接口设计 // 线程池 class TThreadPool { public:TThreadPool() {}TThreadPool(const TThreadPool) delete;TThreadPool operator(const TThreadPool other) delete;~TThreadPool();bool add_thread(std::string name); // add one thread into poolbool delete_thread(std::string name); // remove one thread from poolTThread* get_thread_by_name(std::string threadname); // get the thread by name, dont delete return object by yourselfbool append_task_by_thread(const std::string threadname, const std::functionvoid() task); // add task to pointed threadprivate:std::mutex m_mutex;std::mapstd::string, TThread* m_threads; }; TThreadPool类的主要功能是管理创建的线程TThread它是线程的具体实现它提供了增加/删除线程的接口同时给每个线程打上了标签name。 TThread接口设计 // 对std::thread的封装类 class TThread { public:TThread(std::string name);TThread(const TThread other) delete;TThread operator(const TThread other) delete;~TThread();bool push_task(const std::functionvoid() task); // one add taskstd::thread::id get_thread_id(); // for log purposevoid set_max_task_size(int s); // avoid thread too busyint get_task_size(); // get current task numberprivate:void work(); // real work threadvoid notify(); // notify work thread to quit private:std::string s_name;std::atomic_bool b_running;std::thread* p_thread;std::mutex m_mutex;std::queuestd::functionvoid() m_tasks;std::condition_variable m_cond;std::atomicint i_maxTaskSize; }; TThread类的主要功能是分配任务push_task函数和处理任务work函数。 代码实现 TThreadPool类 TThreadPool::~TThreadPool() {std::unique_lockstd::mutex lk(m_mutex);for(auto iterm_threads.begin(); iter!m_threads.end(); iter) {if(iter-second ! nullptr) {delete iter-second;}}m_threads.clear(); }bool TThreadPool::add_thread(std::string name) {std::unique_lockstd::mutex lk(m_mutex);if(m_threads.count(name)) {return false;}auto tt new TThread(name);if(tt nullptr) {return false;}m_threads[name] tt;return true; }bool TThreadPool::delete_thread(std::string name) {std::unique_lockstd::mutex lk(m_mutex);if(m_threads.count(name) 0) {return false;}delete m_threads[name];m_threads.erase(name);return true; }TThread* TThreadPool::get_thread_by_name(std::string threadname) {std::unique_lockstd::mutex lk(m_mutex);if(m_threads.count(threadname) 0) {return nullptr;}return m_threads[threadname]; }bool TThreadPool::append_task_by_thread(const std::string threadname, const std::functionvoid() task) {std::unique_lockstd::mutex lk(m_mutex);if(m_threads.count(threadname) 0) {return false;}auto tt m_threads[threadname];return tt-push_task(task); }TThread类 const int MAX_TASK_SIZE 100; // max task size in one threadTThread::TThread(std::string name) : s_name(name), b_running(true), i_maxTaskSize(MAX_TASK_SIZE) {p_thread new std::thread(TThread::work, this); }TThread::~TThread() {notify(); // notify work thread quitif(p_thread-joinable()) {p_thread-join();}delete p_thread; }bool TThread::push_task(const std::functionvoid() task) {std::unique_lockstd::mutex lk(m_mutex);if(!b_running) {return false;}if(m_tasks.size() i_maxTaskSize.load()) {return false;}m_tasks.push(task);m_cond.notify_one();return true; }std::thread::id TThread::get_thread_id() {return p_thread-get_id(); }void TThread::set_max_task_size(int s) {if(s 0) {return;}i_maxTaskSize.store(s); }void TThread::work() {std::cout std::this_thread::get_id() begin work thread std::endl;while (b_running) { // quit even tasks remainingstd::functionvoid() task;{std::unique_lockstd::mutex lk(m_mutex);if (!m_tasks.empty()) {task m_tasks.front();m_tasks.pop();} else if (b_running m_tasks.empty()) {m_cond.wait(lk);}}if (task)task(); // do the task}std::cout std::this_thread::get_id() end work thread std::endl; }void TThread::notify() {std::unique_lockstd::mutex lk(m_mutex);b_running false;m_cond.notify_one(); // mutex will be released here, therefore another thread would lock it afterward }int TThread::get_task_size() {std::unique_lockstd::mutex lk(m_mutex);return m_tasks.size(); } 使用方式 有两种方式可以调用对应的线程 公共代码 void func1(int i) {std::cout into func1: i std::endl;sleep(2); // simulate real work }TThreadPool thread_pool;thread_pool.add_thread(vdr); // 启动vdr线程thread_pool.add_thread(xgb); // 启动xgb线程 方式一、(先获取线程对象然后对该线程对象添加任务) auto tt thread_pool.getThreadByName(vdr); tt-push_task(std::bind(func1, 2)); tt-push_task(std::bind(func1, 5)); 方式二、直接通过线程池给对应线程添加任务 thread_pool.append_task_by_thread(vdr, std::bind(func1, 2)); thread_pool.append_task_by_thread(vdr, std::bind(func1, 5)); 注 task是std::functionvoid()类型上面的demo是普通函数实现的真实场景应该是类函数实现如下 class A { public:void func(std::string str) {std::cout into A func: str std::endl;} };A a;thread_pool.append_task_by_thread(vdr, std::bind(A::func, a, 2));thread_pool.append_task_by_thread(vdr, std::bind(A::func, a, 5));
http://www.sadfv.cn/news/288297/

相关文章:

  • 外贸网站设计风格wordpress 添加文件
  • 潍坊做网站哪个公司好九一人才网赣州
  • 西安搬家公司收费价目表2021攀枝花网站怎么做seo
  • 怎样提升网站关键词百度推广电话销售话术
  • 外链博客网站我是新手如何做电商
  • 江西省城乡建设网站圣玺企业网站建设
  • 百度网站类型哪个网站可以做销售记录
  • 网站标签管理做网站面临的困难
  • 网站置顶代码建筑工程网站定制
  • 电子商务网站平台不包括开发网站的成本
  • 中国十佳网站建设公司网站需要具备条件
  • 如何给网站做seowordpress怎么清空
  • php做网站需要的软件原神是哪家公司开发的
  • 公司网站建设注意什么房子设计效果图大全
  • 广州百度网站快速排名c语言软件开和网站开发区别
  • 如何用网页制作网站品牌vi设计包含哪些
  • 网站APP注册做任务网页制作教程田田田田田田田田田田田田田田
  • 购物网站开发所用技术wordpress添加 下载
  • 帮人做网站的推广分类达人介绍
  • 网站建设费用的会计企业标准信息公共服务平台官网
  • php网站开发干嘛的多多进宝怎么推广赚钱
  • 制作一个网站需要多少钱网站备案不通过怎么解决
  • 番禺网站制作哪里有wordpress文章发布区
  • 建设一个跟京东一样的网站印度做网站设计
  • 建立网站专业公司龙井建设局网站
  • 图片分页网站模板专业做视频的网站
  • 茂名网站建设咨询网络营销推广方案总结
  • 班组建设网站青岛网站建设机构
  • 电脑用虚拟机做网站网络营销方式对比分析
  • 济南网站开发公司做淘宝网站要求与想法