windows多并发编程尝试

发布于 2019-05-08  843 次阅读


经查资料,多并发编程需要用到一个库

# include < thread>

using namespace std;

线程初始化:

std::thread t(fun);//fun为要多线程运行的函数

等待线程终止:

t.join();//等待名为t的函数终止运行

如下实现了先启动输出first的线程,但由于多并发,second先输出

#include "pch.h"
#include <cstdio>
#include <thread>
#include <windows.h>
using namespace std;
void fun() {
    Sleep(200);
    printf("first task\n");
}

int main() {
    thread t(fun);
    printf("second task\n");
    t.join();
    printf("third task\n");
}

CTFer|NOIPer|CSGO|摸鱼|菜鸡