<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

    Applications use event objects in a number of situations to notify a waiting thread of the occurrence of an event. For example, overlapped I/O operations on files, named pipes, and communications devices use an event object to signal their completion. For more information about the use of event objects in overlapped I/O operations, see Synchronization and Overlapped Input and Output.

    In the following example, an application uses event objects to prevent several threads from reading from a shared memory buffer while a master thread is writing to that buffer. First, the master thread uses the CreateEvent function to create a manual-reset event object. The master thread sets the event object to nonsignaled when it is writing to the buffer and then resets the object to signaled when it has finished writing. Then it creates several reader threads and an auto-reset event object for each thread. Each reader thread sets its event object to signaled when it is not reading from the buffer.

    #define NUMTHREADS 4
    HANDLE hGlobalWriteEvent;
    void CreateEventsAndThreads(void)
    {
    HANDLE hReadEvents[NUMTHREADS], hThread;
    DWORD i, IDThread;
    // Create a manual-reset event object. The master thread sets
    // this to nonsignaled when it writes to the shared buffer.
    hGlobalWriteEvent = CreateEvent(
    NULL,         // no security attributes
    TRUE,         // manual-reset event
    TRUE,         // initial state is signaled
    "WriteEvent"  // object name
    );
    if (hGlobalWriteEvent == NULL) {
    // error exit
    }
    // Create multiple threads and an auto-reset event object
    // for each thread. Each thread sets its event object to
    // signaled when it is not reading from the shared buffer.
    for(i = 1; i <= NUMTHREADS; i++)
    {
    // Create the auto-reset event.
    hReadEvents[i] = CreateEvent(
    NULL,     // no security attributes
    FALSE,    // auto-reset event
    TRUE,     // initial state is signaled
    NULL);    // object not named
    if (hReadEvents[i] == NULL)
    {
    // Error exit.
    }
    hThread = CreateThread(NULL, 0,
    (LPTHREAD_START_ROUTINE) ThreadFunction,
    &hReadEvents[i],  // pass event handle
    0, &IDThread);
    if (hThread == NULL)
    {
    // Error exit.
    }
    }
    }

    Before the master thread writes to the shared buffer, it uses the ResetEvent function to set the state of hGlobalWriteEvent (an application-defined global variable) to nonsignaled. This blocks the reader threads from starting a read operation. The master then uses the WaitForMultipleObjects function to wait for all reader threads to finish any current read operations. When WaitForMultipleObjects returns, the master thread can safely write to the buffer. After it has finished, it sets hGlobalWriteEvent and all the reader-thread events to signaled, enabling the reader threads to resume their read operations.

    VOID WriteToBuffer(VOID)
    {
    DWORD dwWaitResult, i;
    // Reset hGlobalWriteEvent to nonsignaled, to block readers.
    if (! ResetEvent(hGlobalWriteEvent) )
    {
    // Error exit.
    }
    // Wait for all reading threads to finish reading.
    dwWaitResult = WaitForMultipleObjects(
    NUMTHREADS,   // number of handles in array
    hReadEvents,  // array of read-event handles
    TRUE,         // wait until all are signaled
    INFINITE);    // indefinite wait
    switch (dwWaitResult)
    {
    // All read-event objects were signaled.
    case WAIT_OBJECT_0:
    // Write to the shared buffer.
    break;
    // An error occurred.
    default:
    printf("Wait error: %d\n", GetLastError());
    ExitProcess(0);
    }
    // Set hGlobalWriteEvent to signaled.
    if (! SetEvent(hGlobalWriteEvent) )
    {
    // Error exit.
    }
    // Set all read events to signaled.
    for(i = 1; i <= NUMTHREADS; i++)
    if (! SetEvent(hReadEvents[i]) ) {
    // Error exit.
    }
    }

    Before starting a read operation, each reader thread uses WaitForMultipleObjects to wait for the application-defined global variable hGlobalWriteEvent and its own read event to be signaled. When WaitForMultipleObjects returns, the reader thread's auto-reset event has been reset to nonsignaled. This blocks the master thread from writing to the buffer until the reader thread uses the SetEvent function to set the event's state back to signaled.

    VOID ThreadFunction(LPVOID lpParam)
    {
    DWORD dwWaitResult;
    HANDLE hEvents[2];
    hEvents[0] = *(HANDLE*)lpParam;  // thread's read event
    hEvents[1] = hGlobalWriteEvent;
    dwWaitResult = WaitForMultipleObjects(
    2,            // number of handles in array
    hEvents,      // array of event handles
    TRUE,         // wait till all are signaled
    INFINITE);    // indefinite wait
    switch (dwWaitResult)
    {
    // Both event objects were signaled.
    case WAIT_OBJECT_0:
    // Read from the shared buffer.
    break;
    // An error occurred.
    default:
    printf("Wait error: %d\n", GetLastError());
    ExitThread(0);
    }
    // Set the read event to signaled.
    if (! SetEvent(hEvents[0]) )
    {
    // Error exit.
    }
    }
    posted on 2007-08-17 16:20 jinfeng_wang 閱讀(518) 評(píng)論(0)  編輯  收藏 所屬分類: cppZZ
    主站蜘蛛池模板: 久久精品国产精品亚洲蜜月| 久久久久免费精品国产| 久久亚洲精品成人av无码网站| 国产男女猛烈无遮挡免费视频网站| 久久免费国产视频| 中文无码日韩欧免费视频| 亚洲av午夜电影在线观看| 亚洲一区二区三区亚瑟 | 亚洲不卡在线观看| 亚洲国产美女精品久久久久∴| 哒哒哒免费视频观看在线www | 亚洲AV日韩AV天堂一区二区三区| 又大又硬又爽免费视频| 在线精品免费视频| 性xxxxx免费视频播放| 99久热只有精品视频免费观看17| 久久嫩草影院免费看夜色| 美女被羞羞网站免费下载| 亚洲精品无码少妇30P| 亚洲国产系列一区二区三区| 亚洲熟妇av一区二区三区下载| 78成人精品电影在线播放日韩精品电影一区亚洲 | 亚洲一区无码中文字幕| 免费在线观看一级毛片| 国产在线19禁免费观看国产| 日韩在线天堂免费观看| 免费无码AV电影在线观看| 在线观看av永久免费| 国产乱码免费卡1卡二卡3卡| 亚洲精品免费在线视频| 0588影视手机免费看片| 日韩免费一区二区三区在线| 91网站免费观看| 精品久久久久国产免费| 好男人看视频免费2019中文 | 亚洲av无码成人精品国产 | 亚洲三级在线免费观看| 91精品免费在线观看| 成年美女黄网站色大免费视频| 午夜免费福利在线| 又粗又大又猛又爽免费视频 |