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

淄博网站制作设计高端网页自动点击软件

淄博网站制作设计高端,网页自动点击软件,装修app,js验证网站线程的创建 用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.shuangfujiaoyu.com/news/8719.html

相关文章:

  • 如何让域名指向网站app推广方式
  • 怎么制作网站教程步骤视频seo网络优化平台
  • 赵增敏. JSP网站开发详解哪些网站是营销型网站
  • 找程序员做网站青海网站seo
  • 长春做网站建设的公司阿里云域名注册入口官网
  • 成都青羊网站建设郑州网络营销公司哪个好
  • 什么网站做的很好怎么做一个小程序
  • flash做网站的流程优化模型的推广
  • 泉州wap网站制作手机网站优化排名
  • 江西做网站找谁北京seo优化多少钱
  • 做红k线网站网页制作流程
  • 联通做网站白帽seo公司
  • 公司简介模板文字版seo是搜索引擎营销
  • 怎么做网站企业文化栏目电商运营主要工作内容
  • 网站建设销售技巧话术qq推广软件
  • 汕头网站制作公司上海关键词自动排名
  • 利于seo优化的网站站长之家
  • 云南做网站多少钱女排联赛排名
  • 广州网站建设信科公司最新seo黑帽技术工具软件
  • 网站开发课程设计报告互联网营销的方法有哪些
  • 网站定制化服务淮北seo
  • 买域名去哪个网站网络推广平台哪家公司最好
  • 怎么看网站的备案信息百度竞价怎么排名第一
  • 免费网站建设找云狄建设网站的网站首页
  • 云南 网站模版谷歌关键词优化怎么做
  • 做公司的网站有哪些东西吗2345网址导航怎么样
  • 免费java源代码网站做微商怎么找客源加人
  • 网站收录变少app营销模式有哪些
  • 网站开发架设小红书推广策略
  • 阿里巴巴做网站难吗站长工具使用