Each thread has a single id allowing us to distinguish each of them. The std::thread class has a get_id() function returning an unique id for this thread. You can get a reference to the current thread with the std::this_thread variable. This example starts with threads and each of them prints its id:
#include<iostream>
#include<thread>
#include<vector>
using namespace std;
void thread_task()
{
cout << "hello from thread: \t" <<std::this_thread::get_id() << endl;
}
int main()
{
std::vector<std::thread> threads;
for (int i = 0; i < 5;i++)
{
threads.push_back(std::thread(thread_task));
}
for (auto&thread:threads)
{
thread.join();
}
system("pause");
return 0;
}
Starting each thread one after one and then storing them into a vector is a common way to handle several threads. With that, you can easily change the number of threads. Even with a very little sample like this one, the results is not predictable.