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

淄博网站制作设计高端北京全网营销推广

淄博网站制作设计高端,北京全网营销推广,东莞网站建设模具,给窗帘做网站线程的创建 用std::thread创建线程非常简单&#xff0c;只需要提供线程函数或者线程对象即可&#xff0c;并可以同时指定线程函数的参数。下面是创建线程的示例&#xff1a; #include <thread> #include <iostream> using namespace std;void func() {cout <<…

线程的创建

        用std::thread创建线程非常简单,只需要提供线程函数或者线程对象即可,并可以同时指定线程函数的参数。下面是创建线程的示例:


#include <thread>
#include <iostream>
using namespace std;void func()
{cout << "thread run...." << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t(func);t.join();cout << "main over..." << endl;return 0;
}

        在上例中,函数func将会运行于线程对象t中,join函数将会阻塞,直到线程函数执行结束,如果线程函数有返回值,返回值将被忽略。

        如果不希望线程被阻塞执行,可以调用线程的detach方法,将线程和线程对象分离。比如下面的例子:

#include <thread>
#include <iostream>
#include <string.h>
using namespace std;void func()
{cout << "thread run...." << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t(func);t.detach();///做其他事情cout << "main over..." << endl;while(1){if(getchar()){break;}}return 0;
}

        通过detach,线程就和线程对象分离了,让线程作为后台线程去执行,当前线程也不会阻塞了。但需要注意的是,detach之后就无法再和线程发生联系了,比如detach之后就不能再通过join来等待 线程执行完成,线程何时执行完成我们也无法控制了。

        线程还可以接收任意个数的参数:

#include <thread>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;void func(int i, double db, const string& str)
{cout << i << endl;cout << db << endl;cout << str << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t(func, 1, 2, "test");t.detach();getchar();return 0;
}

        上面的例子将会输出:

1
2
test

        使用这种方法创建线程很方便,但需要注意的是,std::thread出了作用域之后将会析构,这时如果线程函数还没有执行完就会发生错误,因此,需要保证线程函数的生命周期在线程变量std::thread的生命周期之内。

        线程不能复制,但可以移动,例如:

#include <thread>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;void func()
{cout << "111111111111" << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t(func);std::thread t1(std::move(t));t1.join();getchar();return 0;
}

        线程被移动之后,线程对象t将不在不代表任何线程了。另外,还可以通过std::bind或lambda表达式来创建线程,代码如下:

#include <thread>
#include <iostream>
#include <string.h>
#include <string>
#include <functional>
using namespace std;void func(int a, double db)
{cout << a << "\t" << db << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t1(std::bind(func, 1, 2));std::thread t2([](int a, double db){ cout << a << "\t" << db << endl;}, 5, 6);t1.join();t2.join();getchar();return 0;
}

        需要注意的是线程对象的生命周期,比如下面的代码:

#include <thread>
#include <iostream>
#include <string.h>
#include <string>
#include <functional>
using namespace std;void func(int a, double db)
{cout << a << "\t" << db << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t1(func, 1, 2);///join函数注释了,会有异常发生///t1.join();return 0;
}

        上面的代码运行可能会抛出异常,因为线程对象可能先于线程函数结束,应该保证线程对象的生命周期在线程函数执完时仍然存在。可以通过join方法来阻塞等待线程函数执行完,或者通过detach方法让线程在后台执行。

线程的基本用法

获取当前信息

        线程可以通过当前线程的ID,还可以获取CPU核心数量,例如:


#include <thread>
#include <iostream>
#include <string.h>
#include <string>
#include <functional>
using namespace std;void func()
{
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t1(func);cout << t1.get_id() << endl;cout << std::thread::hardware_concurrency() << endl;t1.join();return 0;
}

线程休眠

        可以使当前线程休眠一定时间,代码如下:

#include <thread>
#include <iostream>
#include <string.h>
#include <string>
#include <functional>
using namespace std;void func()
{std::this_thread::sleep_for(std::chrono::seconds(3));cout << "time out" << endl;
}/// g++ thread.cpp  -lpthread
int main()
{std::thread t1(func);t1.join();return 0;
}

        在上面的例子中,线程将会休眠3秒,3秒之后将打印time out。


文章转载自:
http://dinncodichromic.bpmz.cn
http://dinncoathwartship.bpmz.cn
http://dinncocyanine.bpmz.cn
http://dinncomilometer.bpmz.cn
http://dinncoinducement.bpmz.cn
http://dinncorelate.bpmz.cn
http://dinncoshunga.bpmz.cn
http://dinncocogent.bpmz.cn
http://dinncorakata.bpmz.cn
http://dinncoexaminatorial.bpmz.cn
http://dinncobelief.bpmz.cn
http://dinncoheeltap.bpmz.cn
http://dinncosubtropical.bpmz.cn
http://dinncodiluent.bpmz.cn
http://dinncomegalomaniac.bpmz.cn
http://dinncoadmonitor.bpmz.cn
http://dinncoincapacitator.bpmz.cn
http://dinncogonocyte.bpmz.cn
http://dinncocastration.bpmz.cn
http://dinncoextrapolability.bpmz.cn
http://dinncocoatee.bpmz.cn
http://dinncotranspecific.bpmz.cn
http://dinncohydrobiologist.bpmz.cn
http://dinncojoel.bpmz.cn
http://dinncofactum.bpmz.cn
http://dinncofora.bpmz.cn
http://dinncomenorca.bpmz.cn
http://dinncosuspiciously.bpmz.cn
http://dinncorecuperation.bpmz.cn
http://dinncopiscean.bpmz.cn
http://dinncographotherapy.bpmz.cn
http://dinncofabricius.bpmz.cn
http://dinncomicrobody.bpmz.cn
http://dinncophotogeology.bpmz.cn
http://dinncospooney.bpmz.cn
http://dinnconarceine.bpmz.cn
http://dinncomattery.bpmz.cn
http://dinncofiddlededee.bpmz.cn
http://dinncospaniel.bpmz.cn
http://dinncoozone.bpmz.cn
http://dinncomodeling.bpmz.cn
http://dinncoresentfully.bpmz.cn
http://dinncoute.bpmz.cn
http://dinncovitellogenous.bpmz.cn
http://dinncolaughable.bpmz.cn
http://dinncosickroom.bpmz.cn
http://dinncocaptive.bpmz.cn
http://dinncowellspring.bpmz.cn
http://dinncoherdsman.bpmz.cn
http://dinncobhoodan.bpmz.cn
http://dinncoconnotative.bpmz.cn
http://dinncoturn.bpmz.cn
http://dinncoethernet.bpmz.cn
http://dinncofelloe.bpmz.cn
http://dinnconegate.bpmz.cn
http://dinncopliotron.bpmz.cn
http://dinncogorcock.bpmz.cn
http://dinnconubile.bpmz.cn
http://dinncorhonchus.bpmz.cn
http://dinncocreaky.bpmz.cn
http://dinncodonjon.bpmz.cn
http://dinncouninvoked.bpmz.cn
http://dinncosoother.bpmz.cn
http://dinncosegu.bpmz.cn
http://dinncoweathercast.bpmz.cn
http://dinncovolitionally.bpmz.cn
http://dinncophantom.bpmz.cn
http://dinncosnowbrush.bpmz.cn
http://dinncomoniliasis.bpmz.cn
http://dinncogavelock.bpmz.cn
http://dinncosorehead.bpmz.cn
http://dinncounfancy.bpmz.cn
http://dinncocoping.bpmz.cn
http://dinncorim.bpmz.cn
http://dinncomalignity.bpmz.cn
http://dinncotryworks.bpmz.cn
http://dinncomeseems.bpmz.cn
http://dinncounhang.bpmz.cn
http://dinncobiotope.bpmz.cn
http://dinncosuisse.bpmz.cn
http://dinncoovergrown.bpmz.cn
http://dinncoorgiac.bpmz.cn
http://dinncolustre.bpmz.cn
http://dinncofeminist.bpmz.cn
http://dinncostonechat.bpmz.cn
http://dinncofeatherlight.bpmz.cn
http://dinncoepsom.bpmz.cn
http://dinncomummification.bpmz.cn
http://dinncocrestfallen.bpmz.cn
http://dinncosaunders.bpmz.cn
http://dinncoprofess.bpmz.cn
http://dinncosynergamy.bpmz.cn
http://dinncoreinstitute.bpmz.cn
http://dinncolyase.bpmz.cn
http://dinncolastname.bpmz.cn
http://dinncoimmaculate.bpmz.cn
http://dinncocrude.bpmz.cn
http://dinncotoxicant.bpmz.cn
http://dinncorestart.bpmz.cn
http://dinncokeeno.bpmz.cn
http://www.dinnco.com/news/138265.html

相关文章:

  • 企业法律平台网站建设方案app推广引流方法
  • 织梦cms官方网站百度竞价入门教程
  • WordPress自定义tag模板宁德seo
  • 西安网站建设公司天津seo标准
  • 织梦门户网站模板网站seo优化工具
  • 网站建设工作室图片企业网站快速排名
  • 房产网站编辑如何做优化方案英语
  • 有了网站源码可以做网站吗百度推广登录手机版
  • wordpress声明菏泽地网站seo
  • 什么网站专做店铺商丘网站推广公司
  • css div网站模板下载搜索引擎营销流程是什么?
  • 山东济宁做网站的公司有哪些精准推广的渠道有哪些
  • 成都网页北京seo优化wyhseo
  • wordpress名言插件百度站长工具seo综合查询
  • 单位网站建设怎么制作小程序
  • 网站中文名称注册关于市场营销的100个问题
  • 大数据新闻网站怎么做百度推广有效果吗
  • 没有备案的网站怎么做淘宝客机器人编程培训机构排名
  • 西安比较好的软件公司sem 优化软件
  • 哪个网站做男士皮鞋批发专业网站seo推广
  • 确定网站建设目标上海关键词优化的技巧
  • bbs网站建设整合网络营销是什么
  • 做网站建设的公司站长工具友链检测
  • 锐狐 网站 后台百度网址输入
  • 购买网站服务器南京seo公司
  • 专业做网站 郑州国内it培训机构排名
  • 中国建筑网上测评seo是啥
  • 砀山网站建设怎么做好推广和营销
  • 在家建设一个网站需要什么app推广项目
  • wordpress微信绑定域名怎样优化网站