用惯了c中的多线程,后来用兼容c++11的编译器,一个c++的多线程变得更简单,用一段代码展示:
#include <iostream>
#include <thread>//管理线程的函数和类
#include <vector>
using namespace std;
void hello()
{
cout<<"hello concurrent world!\n" ;
}
void main()
{
vector<thread> threads;
for (int i = 0 ; i < 5 ; ++i)//开五个线程
{
threads.push_back(thread(hello)) ;
}
for (auto& thread:threads)
{
thread.join() ;
}
}
程序输出结果: