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

个人网站能做什么沈阳网站建设 熊掌号

个人网站能做什么,沈阳网站建设 熊掌号,义乌企业网站设计,做网站制作需要多少钱1)promise(保证)和future的联合使用#xff0c;实现两个线程的数据传递 #include iostream #includethread #includefutureusing namespace std;//promise用法#xff1a;可以给线程一个值#xff0c;而从另一个线程读出该值 // 实现了两个线程的数…1)promise(保证)和future的联合使用实现两个线程的数据传递 #include iostream #includethread #includefutureusing namespace std;//promise用法可以给线程一个值而从另一个线程读出该值 // 实现了两个线程的数据传递 void myPromise(std::promiseint tmp,int num)//promise线程 {//在这里假使处理复杂的计算最后得出一个值并返回num num 20;tmp.set_value(num); }//另一个线程可以得到promise计算后得到的值 void myFuture(std::futureint tmp) {coutmyFuture thread receive value is : tmp.get()endl; }int main() {std::promiseint pro;std::thread th1(myPromise,std::ref(pro),10); //创建promise线程时第二个参数一定要用引用th1.join();std::futureint result pro.get_future();std::thread th2(myFuture,std::ref(result)); //std::ref( pro.get_future())用临时对象不可以th2.join();return 0; } //输出结果myFuture thread receive value is : 30 2) package_task的使用把任务从新包装起来案例代码如下 #include iostream #includethread #includefutureusing namespace std;int func(void) {coutstart----- thread_id is : std::this_thread::get_id()endl;std::this_thread::sleep_for(std::chrono::milliseconds(2000));coutend-------- thread_id is : std::this_thread::get_id()endl;return 5; }//package_task类模板,可以包装任何可调用对象 int main() {std::packaged_taskint() task(func);std::thread th(std::ref(task)); //package_task里面有一个成员函数get_future(),可以返回一个future对象th.join();std::futureint result task.get_future();coutresult.get()endl;return 0; }//std::async和std::future一起配合使用 #include iostream #includethread #includemutex #includelist #includefuture// std::async和std::future的用法 // async是一个函数模板启动一个线程;std::future是类模板当async启动任务后会返回std::future对象 // std::future对象里面有需要的返回值 using namespace std;int func(void) {coutstart----- thread_id is : std::this_thread::get_id()endl;std::this_thread::sleep_for(std::chrono::milliseconds(2000));coutend-------- thread_id is : std::this_thread::get_id()endl;return 5; }//std::launch::deferred参数时等待wait或者get返回,如果没有这两个函数直接运行接下来代码 //std::launch::deferred参数不会创建新线程 //std::future两个成员函数get是等待线程处理完返回结果wait是等待线程处理完不返回结果 int main() {coutmain thread start----- thread_id is : std::this_thread::get_id()endl;std::futureint result std::async(func); //程序不会卡到这里这里存在一个绑定关系cout********main run*********endl;coutresult.get()endl; //程序会卡到这里等待线程返回结果利用的是std::future的成员函数getreturn 0; }//条件变量与互斥量的配合使用方法 #include iostream #includethread #includemutex #includelist #includecondition_variable //条件变量是和互斥量配合使用using namespace std;class A{ public:void inQueue(void){for(int i 0; i10000;i){coutinsert message : iendl;{std::unique_lockstd::mutex uniqueLock(mtx); message.push_back(i);cond.notify_one();}}}void outQueue(void){for(int i 0 ; i10000; i){std::unique_lockstd::mutex uniqueLock(mtx); //自动加锁//wait跟互斥量的关系wait阻塞解锁wait唤醒加锁cond.wait(uniqueLock,[this](){ //如果参数2是false,解锁等待;如果是true,加锁完成后继续执行下面代码if(!message.empty()) return true;elsereturn false; //可以处理虚假唤醒,就是说容器没有数据但被唤醒了执行解锁等待。}); int cmd message.front();message.pop_front(); coutout cmd---------- is :cmdendl; }//出作用域uniqueLock锁释放;} private:std::listint message;std::condition_variable cond;std::mutex mtx;};int main() {A a;std::thread th1(A::inQueue,a);std::thread th2(A::outQueue,a);th1.join();th2.join();return 0; }5)互斥量的使用方法 #include iostream #includethread #includemutex #includelist #includecondition_variableusing namespace std;class A{ public:void inQueue(void){for(int i 0; i10000;i){coutinsert message : iendl;{std::unique_lockstd::mutex uniqueLock(mtx); message.push_back(i);}std::this_thread::sleep_for(std::chrono::milliseconds(1));}}void outQueue(void){for(int i 0; i10000;i){if(!message.empty()){int cmd 0;{std::unique_lockstd::mutex uniqueLock(mtx); cmd message.front();message.pop_front();}coutout cmd is :cmdendl;}else{coutempty---------------i: iendl;}std::this_thread::sleep_for(std::chrono::milliseconds(1));}} private:std::listint message;std::mutex mtx;};int main() {A a;std::thread th1(A::inQueue,a);std::thread th2(A::outQueue,a);th1.join();th2.join();return 0; }单例模式在线程中的使用 #include iostream #includethread #includemutexusing namespace std;std::mutex mtxRes; //单例目的即使多个线程被创建也要保证只创建一个对象所以创建多线程创建对象时要做互斥量条件判断 class A { private:A(){coutA conctructor---endl;} private:static A* m_instance; //声明一个静态私有类指针变量 public:static A* getInstance(){if(m_instance nullptr) //多个线程为了提升速度(双重锁定){std::unique_lockstd::mutex uniquelock(mtxRes);if(m_instance nullptr){m_instance new A();static garb cl;//为了程序退出后能够正常删除m_instance}}return m_instance;}void test(void){couttest------endl;}class garb //为了能正常delete m_instance而设计的内部类{public:~garb(){if(A::m_instance ! nullptr){delete A::m_instance;A::m_instance nullptr;}}}; };A* A::m_instance nullptr;void func(void) {cout-----开始单例模式创建--------endl;A::getInstance()-test();cout-----单例模式创建完成--------endl; }int main() {// A::getInstance()-test();std::thread th1(func);std::thread th2(func); th1.join();th2.join();return 0; }
http://www.pierceye.com/news/403178/

相关文章:

  • 做co的网站商城网站不备案
  • 黄山建设网站公司电话网站下载链接怎么做
  • 开发企业网站多少钱电视剧排行榜百度搜索风云榜
  • 什么网站做软文装修公司报价如何计算
  • 网站开发免费视频播放器应用公园app免费制作
  • 道路建设去什么网站能看到做内贸注册什么网站
  • 代理东莞网站制作公司wordpress前台用户中心代码
  • 做拼团网站下载wap浏览器
  • 网站建设合同文百科阿里云加WordPress建站
  • 服装购物网站排名ppt制作神器
  • 长沙营销策划公司排名如何优化企业网站
  • 北京制卡厂家做卡公司北京制卡网站_北京制卡_北京 去114网wordpress 关闭注册
  • 网站建设技术优势广州天河区医院
  • python和php网站开发中国十大公司排行榜
  • 网站栅格如何建设一个外卖订餐平台网站
  • 浙江省网站建设报价群晖wordpress不成功
  • 音乐网站制作策划书网站建设公司的服务公司
  • 南昌定制网站开发多少钱手机在线网页制作
  • 无锡网站推广$做下拉去118cr高端网站建设苏州
  • 该网站未在腾讯云备案网页界面图
  • cms开源建站系统河北省建设厅管网站
  • 网站优化排名易下拉技术做官网的步骤
  • 网站备案多久服装企业 北京 网站建设
  • 网站建设(信奈辉煌电商)陕西富通建设工程有限公司网站
  • 南昌县住房和城乡建设局网站外海网站如何做网站的推广
  • 重庆网站推广报价wordpress全景图
  • 做那个的网站谁有建立什么指标体系和评价程序规范
  • 新旧网站对比全国建设厅网站
  • 有经验的番禺网站建设做球服的网站有哪些
  • 临泉建设网站互联网行业都有哪些工作