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

淄博网站制作设计高端高级搜索入口

淄博网站制作设计高端,高级搜索入口,小程序首页模板,中商外贸app线程的创建 用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://www.yidumall.com/news/54396.html

相关文章:

  • 最好的网站开发平台网站群发软件
  • 你做网站群好朋友的作文郴州网站建设推广公司
  • 如何制作自己的网站免费腾讯广告联盟官网
  • linux php网站部署如何推广自己的业务
  • 皮具网站源码爱战网关键词挖掘
  • 网站单页在线制作软件如何优化网页加载速度
  • 柳州网站建设工作室自己怎么优化网站排名
  • 百度创建网站吗成都百度网站排名优化
  • 建设网站公司价格公司怎么做网站推广
  • 网站公司怎么查百度收录
  • 上海公司记账seo技术软件
  • 苏州网站建设设计制作公司在线培训系统平台
  • 佛山网站优化美姿姿seo技术培训机构排名前十
  • 网站建设群发广告词手机app免费下载
  • 瑜伽网站设计收录优美图片崩了
  • 物流网站开发策划网站营销推广
  • 长春网站设计哪家好如何提高网站排名的方法
  • 怎样免费设计网站建设优化大师怎么下载
  • 做网站要需要多少钱网络推广员的工作内容和步骤
  • 湛江网站开发企业培训课程名称大全
  • 织梦做网站简单吗杭州推广公司
  • h5网站开发 源码互联网广告营销
  • 怎么更改网站湘潭关键词优化公司
  • 中英企业网站源码18岁以上站长统计
  • 深圳怎么做网站上海网络公司seo
  • 电子商务网站建设讯息智能建站模板
  • wordpress访问格式丢失成都有实力的seo团队
  • 企业信息网seo优化教程下载
  • 网站怎么引入微信支付seo关键词布局技巧
  • 防止网站被采集123网址之家