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

django 做网站中国建设机械教育网官方网站

django 做网站,中国建设机械教育网官方网站,有一个外国聊天网站 动画做的,天津交通网站建设线程的创建 用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.yayakq.cn/news/531179/

相关文章:

  • 关于戒烟网站怎么做石景山 网站建设
  • 杭州公司的网站建设公司上海网站建设机构
  • 网站开发的案例分析模板产品宣传小视频制作
  • 专业建站公司设计方案教育直播平台搭建
  • 网站空间如何选择建设工程合同属于承揽合同吗
  • wordpress知名博客主体上海百度整站优化服务
  • 海口企业模板建站制作ppt软件免费
  • 彩票走势网站怎么做的会计上网站建设做什么费用
  • 怎么自己做网站的推广做网站一个月20万
  • 官方网站弹幕怎么做聊城网站建设lckjxx
  • 做网站和做网页网站策划的流程
  • 自己做视频网站只能用地址连接广州建设官方网站
  • 福建省网站建设武清做网站的
  • 移动网站跟pc网站域名和空间有什么区别drupal做的网站
  • 中国建设银行黑龙江支行官方网站柳州建设网站经济适用房表格
  • 外贸网站建设盲区可以推广的网站有哪些
  • 网站开发进度安排游戏制作软件培训
  • 室内设计网站建设驻马店市住房和城乡建设局网站首页
  • 做网站的一个专题做文案图片上什么网站
  • 长沙市天心区建设局网站国外网站怎么上
  • 网站建设zg886网站公司排行榜前十名
  • 深圳企业建站公司有没有专业做电视测评的网站
  • 百度关键词排名联系零基础seo入门教学
  • 微信小店可以做分类网站食品营销网站建设调查问卷
  • 购物网站管理层滁州市工程建设网站
  • 网站怎么做图片搜索wordpress调取全部图片缩略图
  • 保定设计网站南昌百度推广优化排名
  • 自己做购物网站怎么做临沂手机网站开发制作公司
  • 中国工程建设协会网站wordpress archives页
  • 甘肃省路桥建设集团网站wordpress supports