使用 Windows 中的事件同步线程

Posted

技术标签:

【中文标题】使用 Windows 中的事件同步线程【英文标题】:Synchronising threads using Events in windows 【发布时间】:2011-02-14 06:01:39 【问题描述】:

我需要使用事件来同步线程。首先,我没有在 Windows 中获得事件的概念。我举个例子,我有主线程和辅助线程。我创建了一个名为“write”和“test”的两个事件。在主线程中,我发出了“写”事件的信号并等待“测试”事件。这两个事件都收到了信号。

基本上我正在将一个应用程序从 linux 移植到 windows。 linux 程序使用条件变量向线程发送信号。条件变量在 Windows XP 中不可用。

例如:

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

#define THREADCOUNT 1

HANDLE ghWriteEvent;
HANDLE ghtestEvent;
HANDLE ghThreads[THREADCOUNT];

DWORD WINAPI ThreadProc(LPVOID);

void CreateEventsAndThreads(void)

    int i;
    DWORD dwThreadID;

    ghWriteEvent = CreateEvent(
        NULL,               // default security attributes
        TRUE,               // manual-reset event
        FALSE,              // initial state is nonsignaled
        TEXT("WriteEvent")  // object name
        );

    if (ghWriteEvent == NULL)
    
        printf("CreateEvent failed (%d)\n", GetLastError());
        return;
    

    ghtestEvent = CreateEvent(
        NULL,               // default security attributes
        FALSE,               // manual-reset event
        FALSE,              // initial state is nonsignaled
        TEXT("WriteEvent")  // object name
        );

    if (ghtestEvent  == NULL)
    
        printf("CreateEvent failed (%d)\n", GetLastError());
        return;
    

    // Create multiple threads to read from the buffer.

    for(i = 0; i < THREADCOUNT; i++)
    
        // TODO: More complex scenarios may require use of a parameter
        //   to the thread procedure, such as an event per thread to
        //   be used for synchronization.
        ghThreads[i] = CreateThread(
            NULL,              // default security
            0,                 // default stack size
            ThreadProc,        // name of the thread function
            NULL,              // no thread parameters
            0,                 // default startup flags
            &dwThreadID);

        if (ghThreads[i] == NULL)
        
            printf("CreateThread failed (%d)\n", GetLastError());
            return;
        
    


void WriteToBuffer(VOID)

    DWORD dwWaitResult;
    printf("Main thread writing to the shared buffer...\n");

    printf("Posting Events for %d\n",ghWriteEvent );

    // Set ghWriteEvent to signaled
    if (! SetEvent(ghWriteEvent) )
    
        printf("SetEvent failed (%d)\n", GetLastError());
        return;
    

    dwWaitResult= WaitForSingleObject(
                            ghtestEvent, // event handle
                            INFINITE);    // indefinite wait

    printf("WaitForSingleObject signelled (%d)\n", GetLastError());

    if ( dwWaitResult == WAIT_OBJECT_0)
    printf("Signlled State for %d with ret val : %d\n",ghtestEvent,dwWaitResult );


void CloseEvents()

    // Close all event handles (currently, only one global handle).

    CloseHandle(ghWriteEvent);


int main( void )

    DWORD dwWaitResult;

    CreateEventsAndThreads();

    WriteToBuffer();

    printf("Main thread waiting for threads to exit...\n");

    // The handle for each thread is signaled when the thread is
    // terminated.
    dwWaitResult = WaitForMultipleObjects(
        THREADCOUNT,   // number of handles in array
        ghThreads,     // array of thread handles
        TRUE,          // wait until all are signaled
        INFINITE);

    switch (dwWaitResult)
    
        // All thread objects were signaled
        case WAIT_OBJECT_0:
            printf("All threads ended, cleaning up for application exit...\n");
            break;

        // An error occurred
        default:
            printf("WaitForMultipleObjects failed (%d)\n", GetLastError());
            return 1;
    

    // Close the events to clean up

    CloseEvents();

    return 0;


DWORD WINAPI ThreadProc(LPVOID lpParam)

    // lpParam not used in this example.
    UNREFERENCED_PARAMETER(lpParam);

    DWORD dwWaitResult;

    printf("Thread %d waiting for write event...\n", GetCurrentThreadId());

    dwWaitResult = WaitForSingleObject(
                        ghWriteEvent, // event handle
                        INFINITE);    // indefinite wait

    printf("rcvd event for Write is  %d \n", ghWriteEvent);
    switch (dwWaitResult)
    
        // Event object was signaled
        case WAIT_OBJECT_0:
                WaitForSingleObject(
                                ghtestEvent, // event handle
                                INFINITE);    // indefinite wait

            printf("rcvd event for %d with Error %d\n", ghtestEvent,GetLastError());

            if ( dwWaitResult == WAIT_OBJECT_0)
            
                printf("Test event signaled for second thread \n");
            
            break;

        // An error occurred
        default:
            printf("Wait error (%d)\n", GetLastError());
            printf("Wait error (%d)\n", GetLastError());
            return 0;
    

    printf("Thread %d exiting\n", GetCurrentThreadId());
    return 1;


Output of the program is:
Main thread writing to the shared buffer...
Posting Events for 2012
WaitForSingleObject signelled (183)
Signlled State for 2008 with ret val : 0
Main thread waiting for threads to exit...
Thread 2016 waiting for write event...
rcvd event for Write is  2012 
rcvd event for 2008 with Error 0
Test event signaled for second thread 
Thread 2016 exiting
All threads ended, cleaning up for application exit...

【问题讨论】:

【参考方案1】:

经过长时间的挣扎,我找到了原因。如果使用相同名称创建两个事件,则创建时的第二个事件将引发错误“ERROR_ALREADY_EXISTS (183)。

如果使用不同的名称创建事件,它可以正常工作。检查错误并进行相应的处理是一种很好的编程方式。

ghtestEvent = CreateEvent(
    NULL,               // default security attributes
    FALSE,               // manual-reset event
    FALSE,              // initial state is nonsignaled
    TEXT("WriteEvent")  // object name
    );
 if ( ERROR_ALREADY_EXISTS == GetLastError() )
 
        printf("unable to create event (%d)\n".GetLastError());
        exit(0);
       

问候 约翰尼

【讨论】:

始终检查错误!在您的情况下,为什么需要命名事件。然后你可以匿名。【参考方案2】:

如果您打算使用事件在进程之间进行通信,您只需要命名事件。如果您只是单个进程中的同步线程,则可以将 NULL 作为名称传递。

当我为进程间通信创建命名同步对象时,我通常给它们一个描述性的名称(如“MyCompany - MyApp - WriteEvent”)并添加一个 GUID 以作为衡量标准。您不希望与将其事件命名为“WriteEvent”的其他开发人员发生名称冲突。名称最长可达 MAX_PATH 个字符,不会影响性能。

【讨论】:

以上是关于使用 Windows 中的事件同步线程的主要内容,如果未能解决你的问题,请参考以下文章

Windows 中的进程间同步屏障

windows多线程 事件event

多线程编程之Windows同步方式

Windows API——CREATEEVENT——创建事件

C++多线程同步技巧--- 事件

C++多线程