使用Windows中的事件同步线程

时间:2011-02-14 06:01:39

标签: multithreading winapi

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

基本上我将appli从linux移植到windows。 linux程序使用条件变量来发送线程信号。窗口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...

2 个答案:

答案 0 :(得分:3)

经过长时间的斗争,我发现了原因。如果使用相同的名称创建了两个事件,则创建时的第二个事件将引发错误“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);
 }      

此致 杜琪峰

答案 1 :(得分:1)

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

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