C++多线程

C++的多线程:
基本就完成了如下的问题,在这里插入图片描述
#include
using namespace std;
#include <windows.h>
HANDLE h[5];
int test1(void *p) {
for (int i = 0; i < 100; i++) {
cout << “1”;
Sleep(10);
}
SetEvent(h[0]);
SetEvent(h[1]);
return 0;
}
int test2(void *p) {
WaitForSingleObject(h[0], INFINITE);
for (int i = 0; i < 100; i++) {
cout << “2”;
Sleep(10);
}
SetEvent(h[2]);
return 0;
}

int test3(void *p) {
WaitForSingleObject(h[1], INFINITE);
for (int i = 0; i < 100; i++) {
cout << “3”;
Sleep(10);
}
SetEvent(h[3]);
return 0;
}

int test4(void *p) {
WaitForSingleObject(h[2], INFINITE);
WaitForSingleObject(h[3], INFINITE);
for (int i = 0; i < 100; i++) {
cout << “4”;
Sleep(10);
}
SetEvent(h[4]);
return 0;
}

int test5(void p) {
WaitForSingleObject(h[4], INFINITE);
for (int i = 0; i < 100; i++) {
cout << “5”;
Sleep(10);
}
return 0;
}
void Init() {
HANDLE b[5];
b[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test1, 0, 0, NULL);
b[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test2, 0, 0, NULL);
b[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test3, 0, 0, NULL);
b[3] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test4, 0, 0, NULL);
b[4] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test5, 0, 0, NULL);
CloseHandle(b[0]);
CloseHandle(b[1]);
CloseHandle(b[2]);
CloseHandle(b[3]);
CloseHandle(b[4]);
}
void Deal() {
h[0] = CreateEvent(NULL, false, false, L"");
h[1] = CreateEvent(NULL, false, false, L"");
h[2] = CreateEvent(NULL, false, false, L"");
h[3] = CreateEvent(NULL, false, false, L"");
h[4] = CreateEvent(NULL, false, false, L"");
}
int main(int argc, char
* argv) {
Init();
Deal();
getchar();
}
新手不太会布局,代码应该很好看懂

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值