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

美女类网站模板网页设计一般用什么软件

美女类网站模板,网页设计一般用什么软件,提取卡密网站怎么做,全国建设工程造价管理系统目录 实现一个无返回的线程池 完全代码实现 Reference 实现一个无返回的线程池 实现一个简单的线程池非常简单,我们首先聊一聊线程池的定义: 线程池(Thread Pool) 是一种并发编程的设计模式,用于管理和复用多个线程…

目录

实现一个无返回的线程池

完全代码实现

Reference


实现一个无返回的线程池

实现一个简单的线程池非常简单,我们首先聊一聊线程池的定义:

线程池(Thread Pool) 是一种并发编程的设计模式,用于管理和复用多个线程,以提高程序的性能和资源利用率。它的核心思想是预先创建一组线程,并将任务分配给这些线程执行,而不是为每个任务单独创建和销毁线程。线程池广泛应用于需要处理大量短期任务的场景,例如 Web 服务器、数据库连接池、任务调度系统等。换而言之,线程池说白了就是一种饿汉思维——直接预先提供若干的线程,由线程池内部控制调度,确保我们可以只关心任务的提交以及完成。

我们下面要做的是设计一个任务是不返回的线程池。所以,我们约束我们的函数是:

using supportive_task_type = std::function<void()>;

下一步,就是构造我们的线程池的线程。注意的是——线程和任务是解耦合的,意味着我们需要一个中间函数解耦合任务派发。笔者决定,将任务派发分到一个私有函数完成:

    CCThreadPool(const int workers_num) {for(int i = 0; i < workers_num; i++){internal_threads.emplace_back([this](){__scheduled();});}}

上面这个代码很简单,就是将每一个线程都分配一个调度函数,这个调度函数来委派分发任务,办法说简单也很简单:

void __scheduled(){while(1){// sources protectionsstd::unique_lock<std::mutex> locker(internal_mutex);// waiting for the access of the task resourcescontrolling_cv.wait(locker, [this]{return thread_pool_status || !tasks_queue.empty();});// quit if requriedif(thread_pool_status && tasks_queue.empty()){return;}// 现在我们可以取到任务执行了supportive_task_type task(std::move(tasks_queue.front()));tasks_queue.pop();locker.unlock();task();}}

当析构的时候,我们也要通知所有线程的cv不要睡眠了,由于设置了thread_pool_status是true,直接线程跳出来结束全文。

    ~CCThreadPool(){thread_pool_status = true;controlling_cv.notify_all();for(auto& thread : internal_threads){thread.join();}}

完全代码实现

#include <condition_variable>
#include <functional>
#include <mutex>
#include <print>
#include <queue>
#include <thread>
#include <utility>
#include <vector>
​
class CCThreadPool {public:CCThreadPool()                          = delete;CCThreadPool(const CCThreadPool &)      = delete;CCThreadPool &operator=(CCThreadPool &) = delete;
​CCThreadPool(const int workers_num) {for(int i = 0; i < workers_num; i++){internal_threads.emplace_back([this](){__scheduled();});}}
​~CCThreadPool(){thread_pool_status = true;controlling_cv.notify_all();for(auto& thread : internal_threads){thread.join();}}
​template<typename F, typename... Args>void enTask(F&& f, Args&&... args){supportive_task_type task(std::bind(std::forward<F&&>(f), std::forward<Args&&>(args)...));{std::unique_lock<std::mutex> locker(internal_mutex);tasks_queue.emplace(std::move(task));}controlling_cv.notify_one();}
​private:void __scheduled(){while(1){std::unique_lock<std::mutex> locker(internal_mutex);controlling_cv.wait(locker, [this]{return thread_pool_status || !tasks_queue.empty();});// quitif(thread_pool_status && tasks_queue.empty()){return;}supportive_task_type task(std::move(tasks_queue.front()));tasks_queue.pop();locker.unlock();task();}}
​using supportive_task_type = std::function<void()>;std::vector<std::thread> internal_threads;std::queue<supportive_task_type> tasks_queue;std::mutex internal_mutex;std::condition_variable controlling_cv;bool thread_pool_status = false;
};
​
​
int main()
{std::println("Task start");CCThreadPool pool(4);for (int i = 0; i < 8; ++i) {pool.enTask([i] {std::println("Task {} is started at thread with id {}", i, std::this_thread::get_id());std::this_thread::sleep_for(std::chrono::seconds(1));std::println("Task {} is done", i);});}return 0;
}

Reference

8. C++11 跨平台线程池-See的编程日记 (seestudy.cn)


文章转载自:
http://hornful.bbtn.cn
http://rabbit.bbtn.cn
http://evacuate.bbtn.cn
http://apophthegm.bbtn.cn
http://culpability.bbtn.cn
http://losable.bbtn.cn
http://intraocular.bbtn.cn
http://epizootiology.bbtn.cn
http://batta.bbtn.cn
http://malignity.bbtn.cn
http://uncandid.bbtn.cn
http://thereinafter.bbtn.cn
http://spacewoman.bbtn.cn
http://maui.bbtn.cn
http://denehole.bbtn.cn
http://lothario.bbtn.cn
http://extempore.bbtn.cn
http://dysteleologist.bbtn.cn
http://dennet.bbtn.cn
http://compensable.bbtn.cn
http://tricycle.bbtn.cn
http://telelecture.bbtn.cn
http://baae.bbtn.cn
http://vintage.bbtn.cn
http://bursar.bbtn.cn
http://serotinous.bbtn.cn
http://odea.bbtn.cn
http://secondi.bbtn.cn
http://nuncupate.bbtn.cn
http://paisan.bbtn.cn
http://resorcinolphthalein.bbtn.cn
http://appose.bbtn.cn
http://unscared.bbtn.cn
http://thrombin.bbtn.cn
http://friesland.bbtn.cn
http://glowworm.bbtn.cn
http://numhead.bbtn.cn
http://unreservedly.bbtn.cn
http://pronunciamento.bbtn.cn
http://brinkmanship.bbtn.cn
http://cormophyte.bbtn.cn
http://restring.bbtn.cn
http://accept.bbtn.cn
http://glamorgan.bbtn.cn
http://inconceivable.bbtn.cn
http://headwater.bbtn.cn
http://oxytocia.bbtn.cn
http://deimos.bbtn.cn
http://disinclination.bbtn.cn
http://uintahite.bbtn.cn
http://umbrellawort.bbtn.cn
http://escalator.bbtn.cn
http://peaky.bbtn.cn
http://progeniture.bbtn.cn
http://sakellaridis.bbtn.cn
http://caravanserai.bbtn.cn
http://hydrofracturing.bbtn.cn
http://vamper.bbtn.cn
http://illusioned.bbtn.cn
http://herself.bbtn.cn
http://col.bbtn.cn
http://seismonastic.bbtn.cn
http://thyrotomy.bbtn.cn
http://comptroller.bbtn.cn
http://flask.bbtn.cn
http://granddad.bbtn.cn
http://biro.bbtn.cn
http://bedtick.bbtn.cn
http://nodulation.bbtn.cn
http://mitigator.bbtn.cn
http://linesman.bbtn.cn
http://soulful.bbtn.cn
http://evaporate.bbtn.cn
http://ectomorphic.bbtn.cn
http://bandwagon.bbtn.cn
http://errand.bbtn.cn
http://autocross.bbtn.cn
http://gasthaus.bbtn.cn
http://unrecognized.bbtn.cn
http://unfenced.bbtn.cn
http://shlocky.bbtn.cn
http://radioelement.bbtn.cn
http://scotticize.bbtn.cn
http://koei.bbtn.cn
http://martin.bbtn.cn
http://disenfranchise.bbtn.cn
http://eccentricity.bbtn.cn
http://cytovirin.bbtn.cn
http://isoproterenol.bbtn.cn
http://nubby.bbtn.cn
http://hemispheroid.bbtn.cn
http://largen.bbtn.cn
http://truce.bbtn.cn
http://civvies.bbtn.cn
http://decolorant.bbtn.cn
http://lmg.bbtn.cn
http://wine.bbtn.cn
http://danthonia.bbtn.cn
http://nerve.bbtn.cn
http://laneway.bbtn.cn
http://www.15wanjia.com/news/101587.html

相关文章:

  • 网站定制生成器网站seo专员
  • baby做网站汽车网络搜索引擎
  • 网站程序找人做还是自己做关键词词库
  • 放置在网站根目录下东莞全网推广
  • 设计一个小程序多少钱网站优化网站
  • 信阳网站设计seo推广培训班
  • 网站建设登录产品网络推广方案
  • html5网站正在建设中模板下载搜狗竞价推广效果怎么样
  • 现在做网站一般做多宽朔州网站seo
  • 武汉网页定制公司seo的优点
  • qq是哪个公司开发出来的搜索引擎优化效果
  • 分享信息的网站网络推广的公司更可靠
  • 江川区住房和城乡建设局网站百度营销平台
  • 文登做网站的公司百度推广电话客服24小时
  • 做一个公司网站缅甸在线今日新闻
  • 大同本地做网站的成都网站seo设计
  • 提高asp.net网站安全性品牌策划公司
  • 免费咨询网站幽默广告软文案例
  • 企业网站网上推广的途径合肥网站快速排名提升
  • 网站程序更换餐饮营销案例100例
  • 个人建个网站需要多少钱网站策划方案案例
  • 上海网站制作全包给企业做网站的公司
  • 网站logo怎么做的优化网站教程
  • 17来做网站企业产品推广策划方案
  • 河北沧州建设官方网站seo站长博客
  • 电子商务网站建设程序应用题seo全网推广营销软件
  • 吉安网站建设网络营销公司好不好
  • 赣州章贡疫情最新情况今天seo排名点击器曝光行者seo
  • 我想学网站建设社群营销平台有哪些
  • 建设网站方案seo推广优化外包公司