We will start multiple threads and see how our program behaves when we unleash multiple processor cores to execute parts of its code at the same time:
- At first, we need to include only two headers and then we declare that we use the std and chrono_literals namespaces:
#include <iostream> #include <thread> using namespace std; using namespace chrono_literals;
- In order to start a thread, we need to be able to tell what code should be executed by it. So, let's define a function that can be executed. Functions are natural potential entry points for threads. The example function accepts an argument, i, which acts as the thread ID. This way we can tell which print line came from which thread later. Additionally, we use the ...