#include <iostream> // std::cout
#include <thread> // std::thread
#include <unistd.h>
using namespace std;
void foo()
{
sleep(1);
cout << "foo run.\n";
}
void bar(int x)
{
cout << "bar run.\n";
}
int main()
{
thread first (foo); // spawn new thread that calls foo()
thread second (bar,0); // spawn new thread that calls bar(0)
cout << "main, foo and bar now execute concurrently...\n";
first.join();
second.join();
cout << "foo and bar completed.\n";
return 0;
}
c++线程入门之thread类的使用
于 2022-12-07 22:06:21 首次发布