现在学长让我把一个1300多行的代码改成多线程,%>_<%,然后开始看了一些多线程的指导,链接在此
有一个例子如下所示:
#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;
}
例子说它的输出应该为:

但是我的输出确实乱掉的:

我是在xcode里面编译的哦。
感谢各位指导。如果能指引一下多线程的方向就更感谢啦!