In the previous section, we learned how POSIX provides support for threads. In this section, we will discuss C++ threads, which are largely inspired by POSIX threads. They provide similar functionality while simplifying the APIs in some ways, and also providing type safety.
Exploring C++ threads
The basics of C++ threads
To demonstrate the simplicity of C++ threads, the following example, like the first example in this chapter, creates two threads and then waits for them to finish executing:
#include <thread>
#include <iostream>
void mythread()
{
std::cout << "Hello World\n";
}
main()
{
std::thread t1{mythread};
std::thread t2{mythread};
t1.join();
t2.join();
}
// > g++ -std=c++17 scratchpad...