1#include <thread>
2void foo()
3{
4 // do stuff...
5}
6int main()
7{
8 std::thread first (foo);
9 first.join();
10}
1// thread example
2#include <iostream> // std::cout
3#include <thread> // std::thread
4
5void foo()
6{
7 // do stuff...
8}
9
10void bar(int x)
11{
12 // do stuff...
13}
14
15int main()
16{
17 std::thread first (foo); // spawn new thread that calls foo()
18 std::thread second (bar,0); // spawn new thread that calls bar(0)
19
20 std::cout << "main, foo and bar now execute concurrently...\n";
21
22 // synchronize threads:
23 first.join(); // pauses until first finishes
24 second.join(); // pauses until second finishes
25
26 std::cout << "foo and bar completed.\n";
27
28 return 0;
29}