《C++ Concurrency In Action》part1 HelloWorld

《C++ Concurrency In Action》part1 HelloWorld


准备系统地学习下C++并发和多线程编程。所以在这从基础开始了。


1、简单的多线程——Hello World

#include <iostream>
#include <thread>

void hello()
{
    std::cout<<"Hello Concurrent World\n";
}

int main()
{
    std::thread t(hello);
    t.join();
}

对于应用程序来说,初始线程是main(),但是对于其他线程,可以在 std::thread 对象的构造函数中指定——在本例中,被命名为t的 std::thread 对象拥有新函数hello()作为其初始函数。


下一个区别:与直接写入标准输出或是从main()调用hello()不同,该程序启动了一个全新的线
程来实现,将线程数量一分为二——初始线程始于main(),而新线程始于hello()。

猜你喜欢

转载自blog.csdn.net/sinat_24206709/article/details/77488219