WaitForMultipleObjects 用法

本文展示了如何在Windows环境中使用C++创建多个线程,并通过事件机制实现线程间的通信,当特定事件被触发时,主线程会捕获并处理相应事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <Windows.h>
#include <stdio.h>

const unsigned int THREAD_NUM = 3;
#define DATA_EVENT_SIZE 4        //事件总数
enum data_event_id { One, Two, Three, Four }; //事件标
HANDLE data_event_array[DATA_EVENT_SIZE];   //事件句柄数组

DWORD WINAPI  ThreadFunc(LPVOID p)
{
    printf("I am a child thread 0 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
    Sleep(500);
    SetEvent(data_event_array[One]);
    printf("The child thread 0 which pid is %d quit ...\n", GetCurrentThreadId());
    return 0;
}

DWORD WINAPI  ThreadFunc1(LPVOID p)
{
    printf("I am a child thread 1 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
    Sleep(2000);
    SetEvent(data_event_array[Two]);
    printf("The child thread 1 which pid is %d quit ...\n", GetCurrentThreadId());
    return 0;
}

DWORD WINAPI  ThreadFunc2(LPVOID p)
{
    printf("I am a child thread 2 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
    Sleep(4000);
    SetEvent(data_event_array[Three]);
    printf("The child thread 2 which pid is %d quit ...,After SetEvent Three\n", GetCurrentThreadId());
    SetEvent(data_event_array[Four]);
    printf("The child thread 2 which pid is %d quit ...,After SetEvent Four\n", GetCurrentThreadId());
    return 0;
}

int main()
{
    printf("I am the main thread that pid is %d ...\n", GetCurrentThreadId());  //输出主线程pid
    HANDLE hThread[THREAD_NUM];
    hThread[0] = CreateThread(NULL, 0, ThreadFunc, 0, 0, NULL); // 创建线程
    hThread[1] = CreateThread(NULL, 0, ThreadFunc1, 0, 0, NULL); // 创建线程
    hThread[2] = CreateThread(NULL, 0, ThreadFunc2, 0, 0, NULL); // 创建线程

    //创建初始状态为无信号,触发后自动复位的事件
    data_event_array[One] = CreateEvent(NULL, FALSE, FALSE, NULL);
    data_event_array[Two] = CreateEvent(NULL, FALSE, FALSE, NULL);
    data_event_array[Three] = CreateEvent(NULL, FALSE, FALSE, NULL);
    data_event_array[Four] = CreateEvent(NULL, FALSE, FALSE, NULL);

    int resulut = 0;
    while (1)
    {
        resulut = WaitForMultipleObjects(DATA_EVENT_SIZE, data_event_array, FALSE, INFINITE); //只要有一个线程返回就结束
        Sleep(30);
        if (WAIT_OBJECT_0 == resulut)
        {
            printf("get the event one!\n");
        }
        else if (WAIT_OBJECT_0 + 1 == resulut)
        {
            printf("get the event two!\n");
        }
        else if (WAIT_OBJECT_0 + 2 == resulut)
        {
            printf("get the event three!\n");
        }
        else if (WAIT_OBJECT_0 + 3 == resulut)
        {
            printf("get the event four!\n");
        }
        else if (WAIT_FAILED == resulut)
        {
            printf("The fuction WaitForMultipleObjects error! \n");
        }
    }
    printf("The main thread which pid is %d quit ...\n", GetCurrentThreadId());
    return 0;
}

输出如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值