WaitForMultipleObjects例子

#include <stdio.h>
#include <windows.h>

HANDLE hEvents[2];
DWORD i;

//void wait(DWORD dwEvent);
void wait(char* s);

DWORD WINAPI ThreadProc1( LPVOID lpParam ) {

    //DWORD dwEvent;
    //wait(dwEvent);
    wait("ThreadProc1");

    ::SetEvent(hEvents[1]);
    ::SetEvent(hEvents[0]);

    return 0;

}
DWORD WINAPI ThreadProc2( LPVOID lpParam ) {

    //DWORD dwEvent;
    //wait(dwEvent);
    wait("ThreadProc2");
    return 0;

}
DWORD WINAPI ThreadProc( LPVOID lpParam ) {

    ::SetEvent(hEvents[1]);
    ::SetEvent(hEvents[0]);
     return 0;

}

int main(int argc, char* argv[])  
 {  
    // Create two event objects.

    for (i = 0; i < 2; i++)
    {
        hEvents[i] = CreateEvent(
            NULL,   // default security attributes
            FALSE,  // auto-reset event object
            FALSE,  // initial state is nonsignaled
            NULL);  // unnamed object

        if (hEvents[i] == NULL)
        {
            printf("CreateEvent error: %d/n", GetLastError() );
            ExitProcess(0);
        }
    }

    // The creating thread waits for other threads or processes
    // to signal the event objects.

        CreateThread(
            NULL,              // default security attributes
            0,                 // use default stack size 
            ThreadProc1,        // thread function
            NULL,             // argument to thread function
            0,                 // use default creation flags
            NULL);   // returns the thread identifier

        CreateThread(
            NULL,              // default security attributes
            0,                 // use default stack size 
            ThreadProc2,        // thread function
            NULL,             // argument to thread function
            0,                 // use default creation flags
            NULL);   // returns the thread identifier


        CreateThread(
            NULL,              // default security attributes
            0,                 // use default stack size 
            ThreadProc,        // thread function
            NULL,             // argument to thread function
            0,                 // use default creation flags
            NULL);   // returns the thread identifier


    getchar();

 } 

void wait(char* s){

    DWORD dwEvent;

    dwEvent = WaitForMultipleObjects(
        2,           // number of objects in array
        hEvents,     // array of objects
        TRUE,       // wait for any
        INFINITE);   // indefinite wait

    // Return value indicates which event is signaled.

    switch (dwEvent)
    {
        // hEvent[0] was signaled.
        case WAIT_OBJECT_0 + 0:
            printf("0 %s",s);
            // Perform tasks required by this event.
            break;

        // hEvent[1] was signaled.
        case WAIT_OBJECT_0 + 1:
            // Perform tasks required by this event.
            printf("1 %s", s);
            break;

        // Return value is invalid.
        default:
            printf("Wait error: %d/n", GetLastError());
            ExitProcess(0);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值