Я новичок в С++, и я изучал некоторые кросс-платформенные учебники потоков С++. Я смотрел на это: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/
и пытался выполнить следующий код:
#include <iostream>
#include <thread>
static const int num_threads = 10;
//This function will be called from a thread
void call_from_thread(int tid) {
std::cout << "Launched by thread " << tid << std::endl;
}
int main() {
std::thread t[num_threads];
//Launch a group of threads
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread, i);
}
std::cout << "Launched from the main\n";
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
return 0;
}
Выход, который я получаю, следующий, и я не понимаю, почему:
[email protected]:~/Desktop$ ./ref
Launched by thread Launched by thread Launched by thread Launched by thread Launched by thread 201
Launched by thread 5
Launched by thread 6
4
Launched by thread 7
3
Launched by thread 8
Launched from the main
Launched by thread 9
Я понимаю, что цифры случайны каждый раз, но иногда я не получаю никаких номеров, и мне интересно, почему?