第一章 Hello Word

版权声明:本文为博主原创文章,转载时请注明来源。有问题请e-mail:[email protected] https://blog.csdn.net/u011177305/article/details/52227645

1.4 开始入门

  

#include <iostream>

int main()

{

std::cout << "Hello World\n";

}

  

  • 使用#include<thread>
  • 定义hello()函数

    因为每个线程都必须具有一个初始函数(initial function),新线程的执行在这里开始。

    对于应用程序来说,初始线程是main(),但是对

    于其他线程,可以在 std::thread 对象的构造函数中指定

  • 被命名为t③的 std::thread 对象拥有新函数hello()作为其初始函数。

      

    新的线程启动之后③,初始线继续执行。如果它不等待新线束,它就将自自地继续行到main()的结束,从而结束程序——有可能发生在新线程运行之前。这就是为什么在调用 join() 的原因——详见第2章,这会导致调用线程(在main()中)等待与 std::thread 对象相关联的线程,即这个例子中的t

猜你喜欢

转载自blog.csdn.net/u011177305/article/details/52227645
今日推荐