#include<iostream>
#include <thread>
using namespace std;
#define NUM_THREAD 6 //线程数
const int GO = 1;//前行
const int BACK = -1;//后退
const int STOP = 0;//停止
//多线程实验
void go(int status) {
if (status==GO)
{
cout << "Go direct!" << endl;
}else if (status==BACK)
{
cout << "Go back!" << endl;
}
else
{
cout << "Stop!" << endl;
}
}
void turn_left() {
cout << "turn left!" << endl;
}
void turn_right() {
cout << "Turn right!" << endl;
}
void turn_round() {
cout << "Turn round!" << endl;
}
//实现多个线程运行
int main() {
thread thrs[NUM_THREAD];//创建一个线程组,用于保存要创建的线程
thrs[0] = thread(go, GO);//前行
thrs[1] = thread(go, BACK);//后退
thrs[2] = thread(go, STOP);//停止
thrs[3] = thread(turn_left);//左转
thrs[4] = thread(turn_right);//右转
thrs[5] = thread(turn_round);//掉头
for (int i=0;i<6;i++)
{
thrs[i].join();
}
system("pause");
return 0;
}
运行结果: