Thread Synchronization using events

Discussion in 'MFC' started by d_arin100, Oct 15, 2009.

  1. d_arin100

    d_arin100 New Member

    Joined:
    Sep 25, 2009
    Messages:
    10
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Bangalore

    Introduction



    Event is one type of kernel object that Windows provides for thread synchronization. Here I shall describe the Win32 event as well as MFC event. MFC's CEvent class encapsulates Win32 event objects. Generally events are used in cases when one thread is supposed to start its job after a specified job has over. For example, a thread might wait until the necessary data is read and then start writes in the hard disk.

    Background



    There are two kinds of events (i) manual reset and another is (ii) auto reset. By using an event we simply can notify another thread that a specified action has occurred. An event can be either in the signaled (or true) state or in the nonsignaled (or false) state. A manual reset eventobject stays in the state unless and until the [inlincecode]SetEvent()[/inlinecode] or ResetEvent()[/inlinecod] function is called. But in auto reset eventobject automatically returns to a nonsignaled (unavailable) state after at least one thread is released.

    Win32 Event Object:



    The CreateEvent() function is used to create an event thread synchronization object. The manual or auto reset event can be mentioned at the CreateEvent() function parameter. The WaitForSingleObject()or WaitForMultipleObjects() are use for waiting thread for a particular event occurs. The CreateEvent() function is used to create named and unnamed event objects and also we can specify the initial state of the event object. Now if the named event is already available, then the OpenEvent() function is used to access the event previously created by the CreateEvent() function. The SetEvent() function is used to set the event object to signal state and the ResetEvent() function is used to set the event object to non-signaled state.

    Code:
     
    HANDLE g_Event; 
    /// Creating a manual reset event 
    g_Event = CreateEvent(NULL, TRUE, TRUE, "MyEvent");  
    ResetEvent(g_Event); 
    /// If reading complete
    SetEvent(g_Event);
    

    MFC Event Object::



    MFC's CEvent class encapsulates Win32 event objects. The CEvent class is derived from the abstract CSyncObject Class. The CSyncObject is derived from the CObject. The CEvent constructor specifies the manual or auto reset event options.
    Code:
    CEvent (BOOL bInitiallyOwn = FALSE, 
    	BOOL bManualReset = FALSE, LPCTSTR lpszName = NULL,
    	LPSECURITY_ATTRIBUTES lpsaAttribute = NULL)
    
    The Manual event object signaled/non-signaled uses SetEvent() or ResetEvent() member function.

    Now the question is when we choose auto reset or manual reset events:

    i) We use auto reset event when only one thread will be triggered by the event and release the waiting thread with SetEvent(). There's no need to call ResetEvent() because the event is reset automatically when the thread is released.
    ii) We use manual reset event when two or more threads will be triggered by the event.

    It is important to use PulseEvent() to pull the trigger on a manual reset event. Now if we use PulseEvent() then also no need to call ResetEvent() because PulseEvent() resets the event after releasing the threads. If we use SetEvent() and ResetEvent(), then there is no guarantee that all waiting threads will be released. But PulseEvent() sets and resets the event as well as ensures that all the waiting threads on the event are released before resetting the event.

    Code:
    // CEvent auto reset object initially nonsignaled
    CEvent g_event;  
    /// First thread release for second thread
    g_event.SetEvent ();   
    /// Second thread wait for a signal
    g_event.Lock ();
    
    In the above code second thread calls Lock() member function to block on the event object. The second thread blocks until the first thread sets the event.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the info, I appreciate it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice