多线程
#include#include using namespace std; void TestMultiThread(int no_thread) { cout << "No." << no_thread << endl; } void MultiThreads(const in num_threads) { thread multi_threads[num_threads]; for (int i = 0; i < num_threads; ++i) { multi_threads[i] = thread(TestMultiThread, i); // multi_threads[i].join(); // 放在这的话结果顺序输出,直观感觉像是没有多线程。。。(是因为下个线程加入之前上个线程就执行完了吗?) } for (int i = 0; i < num_threads; ++i) { multi_threads[i].join(); // 这样貌似就给人直观的感受~~ } } int main() { MultiThreads(10); return 0; }
通过thread调用的函数不支持传引用(改用指针就没问题了~)
编译时注意加 -std=c++11