C++多线程:实现小车复杂运行

#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;
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值