#include<thread>
#include<iostream>
#include<stdlib.h>
#include<windows.h>
#include<vector>
using namespace std;
void getMsg()
{
while (1)
{
vector<int> rand_num;
int ran = rand();
rand_num.push_back(ran);
cout << "first thread: " << rand() << "\n" << endl;
Sleep(5000);
ran++;
if (ran == 5)
{
break;
}
}
return;
}
int main(int argc, char *argv[])
{
std::string sendBuf;
thread trdRecv(getMsg); //创建子线程以完成getMsg
while (1)
{
for (int i = 0; i<5; i++)
{
cout <<"second thread: "<< rand() <<"\n" <<endl;
Sleep(5000);
}
}
trdRecv.join(); // 子进程结束后合并到主进程,自己删除占用内存
//trdRecv.detach(); // 子进程结束后不并到主进程,自己删除占用内存
return 0;
}