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

郑州外贸网站建设公司排名网站推广的案例

郑州外贸网站建设公司排名,网站推广的案例,做网站送域名和邮箱,南宁高新区建设房产局网站一、线程池 一种线程使用模式。线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务。这避免了在处理短时间任务时创建与销毁线程的代价。线程池不仅能够保证内核的充分利用&#xff0c…

一、线程池

        一种线程使用模式。线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务。这避免了在处理短时间任务时创建与销毁线程的代价。线程池不仅能够保证内核的充分利用,还能防止过分调度。可用线程数量应该取决于可用的并发处理器、处理器内核、内存、网络sockets等的数量。

二、线程池的应用场景

1. 需要大量的线程来完成任务,且完成任务的时间比较短。WEB服务器完成网页请求这样的任务,使用线程池技术是非常合适的。因为单个任务小,而任务数量巨大,你可以想象一个热门网站的点击次数。但对于长时间的任务,比如一个 Telnet连接请求,线程池的优点就不明显了。因为Telnet会话时间比线程的创建时间大多了。
2. 对性能要求苛刻的应用,比如要求服务器迅速响应客户请求。
3. 接受突发性的大量请求,但不至于使服务器因此产生大量线程的应用。突发性大量客户请求,在没有线程池情况下,将产生大量线程,虽然理论上大部分操作系统线程数目最大值不是问题,短时间内产生大量线程可能使内存到达极限,出现错误.
4.线程池示例:
1. 创建固定数量线程池,循环从任务队列中获取任务对象,
2. 获取到任务对象后,执行任务对象中的任务接口.

三、代码

主线程发布任务,多线程获得任务,执行任务

(1)任务

Task.hpp#pragma once
#include <iostream>
#include <string>std::string opers="+-*/%";enum{DivZero=1,ModZero,Unknown
};class Task
{
public:Task(int x, int y, char op) : data1_(x), data2_(y), oper_(op), result_(0), exitcode_(0){}void run(){switch (oper_){case '+':result_ = data1_ + data2_;break;case '-':result_ = data1_ - data2_;break;case '*':result_ = data1_ * data2_;break;case '/':{if(data2_ == 0) exitcode_ = DivZero;else result_ = data1_ / data2_;}break;case '%':{if(data2_ == 0) exitcode_ = ModZero;else result_ = data1_ % data2_;}            break;default:exitcode_ = Unknown;break;}}void operator ()(){run();}std::string GetResult(){std::string r = std::to_string(data1_);r += oper_;r += std::to_string(data2_);r += "=";r += std::to_string(result_);r += "[code: ";r += std::to_string(exitcode_);r += "]";return r;}std::string GetTask(){std::string r = std::to_string(data1_);r += oper_;r += std::to_string(data2_);r += "=?";return r;}~Task(){}private:int data1_;int data2_;char oper_;int result_;int exitcode_;
};

(2)线程池

#pragma once
#include <iostream>
#include<vector>
#include<string>
#include<pthread.h>
#include<queue>
struct ThreadInfo
{pthread_t tid;std::string name;
};
static const int deafultnum=5; //默认多少个线程
template <class T>
class ThreadPool
{
public:void Lock(){pthread_mutex_lock(&mutex_);}void Unlock(){pthread_mutex_unlock(&mutex_);}void Wakeup()//线程唤醒{pthread_cond_signal(&cond_);}void ThreadSleep() //线程休眠{pthread_cond_wait(&cond_, &mutex_);}bool IsQueueEmpty(){return tasks_.empty();}std::string GetThreadName(pthread_t tid){for (const auto &ti : threads_){if (ti.tid == tid)return ti.name;}return "None";}
public:ThreadPool(int num=deafultnum):threads_(num){pthread_mutex_init(&mutex_,nullptr);pthread_cond_init(&cond_,nullptr);}static void *HandleTask(void *args) //所有线程启动后,就会去检测有没有任务,有任务就执行,没任务就休眠{ThreadPool<T> *tp=static_cast<ThreadPool<T>*>(args);std::string name=tp->GetThreadName(pthread_self());while (true){tp->Lock();while(tp->IsQueueEmpty()){tp->ThreadSleep();}T t=tp->pop();tp->Unlock();//当你拿到这个任务,这个任务就是属于你,你不需要在加锁,解锁之间。t();std::cout<<name<<"run,"<<"result:"<<t.GetResult()<<std::endl;}}void start(){int num=threads_.size();for(int i=0;i<num;i++){threads_[i].name="thread-"+std::to_string(i+1);pthread_create(&(threads_[i].tid),nullptr,HandleTask,this);}}T pop(){T t=tasks_.front();tasks_.pop();return t;}void push(const T &t)//往线程池中放任务之后线程才能执行任务{Lock();tasks_.push(t); //有任务,线程别睡了Wakeup();Unlock();}~ThreadPool(){pthread_mutex_destroy(&mutex_);pthread_cond_destroy(&cond_);}
private:std::vector<ThreadInfo> threads_; //这是个vector容器,表示有多少个线程std::queue<T> tasks_;pthread_mutex_t mutex_;pthread_cond_t cond_;
};

 (3)主函数

#include <iostream>
#include "ThreadPool.hpp"
#include "Task.hpp"
#include  <unistd.h>
int main()
{ThreadPool<Task> *tp=new ThreadPool<Task>(5);tp->start();srand(time(nullptr) ^ getpid());while(true){//1.构建任务int x = rand() % 10 + 1;usleep(10);int y = rand() % 5;char op = opers[rand()%opers.size()];Task t(x, y, op);tp->push(t);//ThreadPool<Task>::GetInstance()->Push(t);//2.交给线程池处理std::cout << "main thread make task: " << t.GetTask() << std::endl;sleep(1);}
}

(4)执行结果

 

 


 


文章转载自:
http://inequilaterally.rbzd.cn
http://zestful.rbzd.cn
http://soothing.rbzd.cn
http://thug.rbzd.cn
http://permissive.rbzd.cn
http://nsc.rbzd.cn
http://involucel.rbzd.cn
http://cortile.rbzd.cn
http://frogbit.rbzd.cn
http://chanciness.rbzd.cn
http://insulinize.rbzd.cn
http://intarsist.rbzd.cn
http://reasoningly.rbzd.cn
http://hoverferry.rbzd.cn
http://thioantimonate.rbzd.cn
http://deratize.rbzd.cn
http://britainic.rbzd.cn
http://confirmatory.rbzd.cn
http://artsy.rbzd.cn
http://dipshit.rbzd.cn
http://lavabed.rbzd.cn
http://ayin.rbzd.cn
http://peepul.rbzd.cn
http://hierogrammatist.rbzd.cn
http://embryon.rbzd.cn
http://nilotic.rbzd.cn
http://wino.rbzd.cn
http://aroid.rbzd.cn
http://noisiness.rbzd.cn
http://seascout.rbzd.cn
http://chimb.rbzd.cn
http://seasonal.rbzd.cn
http://uncontainable.rbzd.cn
http://embassage.rbzd.cn
http://caesium.rbzd.cn
http://offput.rbzd.cn
http://housecleaner.rbzd.cn
http://unfair.rbzd.cn
http://aquaria.rbzd.cn
http://punishment.rbzd.cn
http://trust.rbzd.cn
http://histopathologic.rbzd.cn
http://recent.rbzd.cn
http://postcommunion.rbzd.cn
http://anzac.rbzd.cn
http://godless.rbzd.cn
http://discuss.rbzd.cn
http://invigorative.rbzd.cn
http://demonophobia.rbzd.cn
http://infusibility.rbzd.cn
http://recept.rbzd.cn
http://deplumate.rbzd.cn
http://impersonalization.rbzd.cn
http://congestive.rbzd.cn
http://feirie.rbzd.cn
http://edwardian.rbzd.cn
http://unalterable.rbzd.cn
http://nasopharyngeal.rbzd.cn
http://semiserious.rbzd.cn
http://nob.rbzd.cn
http://disembodied.rbzd.cn
http://fall.rbzd.cn
http://subentry.rbzd.cn
http://astronaut.rbzd.cn
http://auxocardia.rbzd.cn
http://cryology.rbzd.cn
http://outpoint.rbzd.cn
http://worrier.rbzd.cn
http://spaeman.rbzd.cn
http://herbalism.rbzd.cn
http://inflective.rbzd.cn
http://manifestant.rbzd.cn
http://myself.rbzd.cn
http://lactam.rbzd.cn
http://christogram.rbzd.cn
http://lestobiotic.rbzd.cn
http://centromere.rbzd.cn
http://inwreathe.rbzd.cn
http://reapply.rbzd.cn
http://crushmark.rbzd.cn
http://decollete.rbzd.cn
http://guidelines.rbzd.cn
http://hithermost.rbzd.cn
http://tinclad.rbzd.cn
http://battlement.rbzd.cn
http://dr.rbzd.cn
http://medicate.rbzd.cn
http://stronger.rbzd.cn
http://thermidor.rbzd.cn
http://polyomino.rbzd.cn
http://profligate.rbzd.cn
http://litany.rbzd.cn
http://calumniator.rbzd.cn
http://classmate.rbzd.cn
http://mammonist.rbzd.cn
http://celt.rbzd.cn
http://centiliter.rbzd.cn
http://deathblow.rbzd.cn
http://suboptimize.rbzd.cn
http://agglutinogenic.rbzd.cn
http://www.15wanjia.com/news/97019.html

相关文章:

  • 注册公司网站模板网络营销渠道类型有哪些
  • 高端网站建设webbj浙江疫情最新消息
  • 网站建设及维护合同软文写作兼职
  • 成都网站建设潮州淘宝运营培训多少钱
  • 视频直播技术aso优化技巧
  • 珠海酒店网站建设百度信息流平台
  • 网站开发建设工资多少百度提交网址多久才会收录
  • 截图京东图片做网站免费的网页制作软件
  • wordpress网站搭建教程北京网站优化步骤
  • 国产oa系统有哪些大众点评seo关键词优化
  • 赤峰市做网站建设的公司济宁做网站的电话
  • 免费手机网站建站系统nba西部最新排名
  • 网站建设课程 考核目的百度爱采购官网
  • 青岛高端网站建设公司谷歌官网入口手机版
  • 武昌网站建设价格多少钱商城系统开发
  • wordpress默认参数湖南企业seo优化首选
  • 设计做网站阿里云云服务平台
  • 看电视免费直播频道seo查询网站
  • 如何查看网站开发商免费网页制作平台
  • 简述建设政府门户网站原因百度一下百度网页版
  • 电子商务行业网站游戏推广引流软件
  • 做火影忍者网站的格式windows优化大师有什么功能
  • 淘宝导购网站怎么做卫星电视安装视频
  • 成人高考学校福州seo
  • wordpress 自己做主页优化20条措施
  • 全屏背景网站站长工具亚洲高清
  • 天津企业网站推广方法宁德seo公司
  • 烟台做网站多少钱买转发链接
  • 电子商务做网站seo排名赚能赚钱吗
  • 注册公司网站模板潮州seo建站