首页 > 代码库 > Event

Event

// Event.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <assert.h>


DWORD WINAPI ThreadProc1(LPVOID lpThreadParameter);
DWORD WINAPI ThreadProc2(LPVOID lpThreadParameter);

int main()
{
    HANDLE hEvent = CreateEventA(0, 1, 0, "Event0");        // 手动 无信号
    if (!hEvent) { assert(0); }        
    SetEvent(hEvent);                                        // 有信号

    HANDLE hThread[2]{};
    hThread[0] = CreateThread(0, 0, ThreadProc1, 0, 0, 0);
    hThread[1] = CreateThread(0, 0, ThreadProc2, 0, 0, 0);

    WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
    CloseHandle(hThread[0]);
    CloseHandle(hThread[1]);

    system("pause");

    return 0;
}


DWORD WINAPI ThreadProc1(LPVOID lpThreadParameter)
{
    HANDLE hEvent = OpenEventA(EVENT_ALL_ACCESS, 0, "Event0");        // 没影响
    if (!hEvent) { assert(0); }
    WaitForSingleObject(hEvent, INFINITE);                            // 因为是手动的,所以还是有信号
    ResetEvent(hEvent);                                                // 无信号

    return 0;
}

DWORD WINAPI ThreadProc2(LPVOID lpThreadParameter)
{
    HANDLE hEvent = OpenEventA(EVENT_ALL_ACCESS, 0, "Event0");
    if (!hEvent) { assert(0); }
    WaitForSingleObject(hEvent, INFINITE);

    return 0;
}

 

Event