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

增加网站备案网站页面优化分析

增加网站备案,网站页面优化分析,100个经典创意营销方案,高校后勤网站建设要求C多线程类Thread#xff08;C11#xff09; C11中std命名空间将Boost库中的Thread加入#xff0c;Boost的多线程从准标准变为标准#xff0c;这里将其用法整理复习#xff0c;以demo的形式复习#xff0c;还是喜欢看我自己写的东西#xff0c;符合我的个人逻辑头文件为…C多线程类ThreadC11 C11中std命名空间将Boost库中的Thread加入Boost的多线程从准标准变为标准这里将其用法整理复习以demo的形式复习还是喜欢看我自己写的东西符合我的个人逻辑头文件为#includethread,通过std::thread应用。就以Hello thread开始吧需要注意的是1join()函数和detach()函数的区别,2数据同步操作mutex需包含includemutex互斥锁 1、  普通函数多线程调用 1无参数函数 #include thread #include iostreamvoid hello_thread() {std::cout Hello Thread! std::endl; }int main() {std::thread t1(hello_thread); t1.join(); // 主线程等待子线程执行结束之后才会执行下一步的操作std::cout Main here std::endl;getchar();return 0; } 2有参数函数 #include thread #include iostreamusing namespace std;int msum(int a, int b) {int c a b;cout a b c endl;return c; }int main() {std::thread t1(msum,1,2);t1.join(); // 主线程等待子线程执行结束之后才会执行下一步的操作std::cout Main here std::endl;getchar();return 0; } 2、在类内部创建线程 1类内部函数为静态函数 #include thread #include iostreamusing namespace std;class HelloThread{public:static void hellothread(){cout Hello World, I‘m a thread endl;}static void start(){thread thread(hellothread);thread.join();}};int main(){HelloThread::start();getchar();return 0; } 在这里start()和hellothread()方法都必须是static方法。  2在Singleton模式内部创建线程  3 、用类内部函数在类外部创建线程 非常普通的类只是用多线程调用其内部的函数 #include thread #include iostreamusing namespace std;class HelloWorld{public:void hello(int year){cout I am year years old! endl;} };int main(){HelloWorld obj;thread thrd(HelloWorld::hello,obj,26);thrd.join();getchar();return 0; } join和detach的区别 join的作用前面已经提到主线程等待子线程结束方可执行下一步串行detach是的子线程放飞自我独立于主线程并发执行主线程后续代码段无需等待。看看效果 1join() #include thread #include iostreamusing namespace std;void hello_thread() {std::cout Hello Thread! std::endl; }int msum(int a, int b) {int c a b;cout a b c endl;return c; }int main(){thread t1(hello_thread);//主线程等待子线程运行结束之后才可以执行下一步t1.join();thread t2(msum,2,3);t2.join();//主线程代码cout Main here endl;getchar();return 0; } 2detach #include thread #include iostreamusing namespace std;void hello_thread() {std::cout Hello Thread! std::endl; }int msum(int a, int b) {int c a b;cout a b c endl;return c; }int main(){thread t1(hello_thread);//主线程等待子线程运行结束之后才可以执行下一步t1.detach();thread t2(msum,2,3);t2.detach();//主线程代码cout Main here endl;getchar();return 0; } 数据同步线程同时操作一个数据的安全性 执行单个线程上锁、解锁维护了线程的安全性防止线程之间交叉执行 #include thread #include iostream #include mutex using namespace std; std::mutex mt; int data1 1;void addmethod(int a){mt.lock();data1 a;cout add data data1 endl;mt.unlock(); } void multi(int a){mt.lock();data1 * a;cout multi data data1 endl;mt.unlock(); } void hello_thread() {std::cout Hello Thread! std::endl; }int msum(int a, int b) {int c a b;cout a b c endl;return c; }int main(){thread t1(addmethod,2);thread t2(multi,2);t1.detach();t2.detach();getchar();return 0; } 线程暂停 从外部让线程暂停会引发很多并发问题此处不做引申。这大概也是std::thread并没有直接提供pause函数的原因。但有时线程在运行时确实需要“停顿”一段时间怎么办呢可以使用std::this_thread::sleep_for或std::this_thread::sleep_until #include thread #include iostream #include mutex #include chrono #include ratio using namespace std; std::mutex mt;void pausable(){//sleep 500毫秒this_thread::sleep_for(chrono::milliseconds(500));//sleep 到达指定的时间点this_thread::sleep_until(chrono::system_clock::now() chrono::milliseconds(500)); } int main(){std::thread thread1(pausable);thread1.join();return 0; } 线程停止 一般情况下当线程函数执行完成后线程“自然”停止。但在std::thread中有一种情况会造成线程异常终止那就是析构。当std::thread实例析构时如果线程还在运行则线程会被强行终止掉这可能会造成资源的泄漏因此尽量在析构前join一下以确保线程成功结束。如果确实想提前让线程结束怎么办呢一个简单的方法是使用“共享变量”线程定期地去检测该量如果需要退出则停止执行退出线程函数。使用“共享变量”需要注意在多核、多CPU的情况下需要使用“原子”操作。 参考链接 https://blog.csdn.net/ouyangfushu/article/details/80199140
http://www.yutouwan.com/news/99308/

相关文章:

  • 网站结构说明烟台做网站多少钱
  • 南昌企业做网站中文网页设计模板下载
  • 网站建设之网页制作语言基础阿里外贸平台
  • 网站相似度检测 站长怎么免费建设金融网站
  • 只做画册的网站常州网签备案查询
  • 平面设计跟网站建设南方医科大学精品课程建设网站
  • 网站内外链接怎么做企业展厅设计公司盛世笔特
  • 移动网站开发课程设计网络营销推广平台有哪些
  • 网站建设体质喝什么茶广元网站建设工作室
  • 一键建站平台开发平台游戏
  • 大型网站建设公司有哪些贸易公司做推广的网站
  • 南京 做网站响应式网站应该怎么做
  • xampp网站后台微信开发平台公司
  • 想美团这样的网站怎么做的门户网站是指提供什么的网站
  • 宁波网站建设制作价格室外建筑网站
  • 酒店网站开发需求是企业写的吗网站制作公司网站建设公司
  • 河南建设网站公司传奇高爆网页游戏
  • 长春建站公司模板整站优化排名
  • 定制 网站开发 价格校园网络拓扑图及网络方案
  • 阿里云网站建设方案书模板ui设计公司
  • 凡科可以做淘宝客网站吗广州百度关键词排名
  • 大庆做网站的上海网站建设流程
  • 网站制作优化全包徐州制作网站软件
  • 没有备案的网站怎么访问类qq留言网站建设
  • 网站建设 东八区网站建设 柳州
  • 制作网站哪家服务好卢松松博客主题 wordpress
  • 快速提高网站权重提升燃气公司网站建设方案
  • 重庆免费推广网站在线开发培训网站建设
  • 广州建站网站如何wordpress建站
  • 网站建设服务合同印花税wordpress wamp