多线程跑(c++)进行测试
#include <stdio.h>
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void myPrint(int id)
{
for (int j = 0; j < 100; j++)
{
for (int i = 0; i < 10; i++)
{
printf("111");
}
sleep(1);
}
}
void startThread()
{
// 创建多个线程
vector<thread> mythreads;
// 创建500个线程,入口函数都是myPrint,id = i
for (int i = 0; i < 500; i++)
{
mythreads.push_back(thread(myPrint, i)); // 创建线程并开始执行
}
for (vector<thread>::iterator it = mythreads.begin(); it != mythreads.end(); it++)
{
it->join(); // 主线程等待子线程
}
cout << "主线程执行完毕!" << endl;
}
int main() {
// 拉起500个线程
startThread();
return 0;
}