34 lines
487 B
C++
Executable file
34 lines
487 B
C++
Executable file
|
|
#include <thread>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <chrono>
|
|
|
|
/*
|
|
* Compile with 'g++ -pthread a.cpp'
|
|
*
|
|
* The order of printing is not well-defined
|
|
*/
|
|
|
|
using namespace std;
|
|
|
|
void f1() {
|
|
cout << "Hello " << endl;
|
|
this_thread::sleep_for(std::chrono::seconds(2));
|
|
cout << "World" << endl;
|
|
}
|
|
|
|
void f2() {
|
|
this_thread::sleep_for(std::chrono::seconds(1));
|
|
cout << "Parallel" << endl;
|
|
}
|
|
|
|
int main() {
|
|
thread t1(f1);
|
|
thread t2(f2);
|
|
|
|
t1.join();
|
|
t2.join();
|
|
|
|
return 0;
|
|
}
|