Thread: SetEvent() and ResetEvent()?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    SetEvent() and ResetEvent()?

    I've never used events directly, so I'm not quite sure what the point of this code is? I see some code where they call SetEvent() immediately followed by ResetEvent().
    As usual, there's no comments saying what it's supposed to be doing, so can anyone tell me why someone might want to signal & unsignal an event like that?
    Code:
    hEvent = OpenEvent( EVENT_ALL_ACCESS, TRUE, rt[i].DllResponderEvent );
    BOOL serc = SetEvent( hEvent );
    
    if ( serc == FALSE )
    {
        // Log an error.
    }
    
    ResetEvent( hEvent );
    CloseHandle( hEvent );

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Is this in some sample code? Could be that the Set and Reset lines actually have other code in between them. Alternatively, it could be a really funky way of checking that the Event was created properly (again, followed by some arbitrary lines of code before the Reset). Either way, this entire code section doesn't really do anything except create an event and destroy it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Hunter2 View Post
    Is this in some sample code? Could be that the Set and Reset lines actually have other code in between them. Alternatively, it could be a really funky way of checking that the Event was created properly (again, followed by some arbitrary lines of code before the Reset). Either way, this entire code section doesn't really do anything except create an event and destroy it.
    Nope, it's actually in production software and it doesn't have any code in between except for some error logging if SetEvent() failed. There's a LOT of things like that in this code that make me scratch my head...

    So basically, you're saying this code has no purpose?
    I'm not gonna mess around with it one way or the other, but I'd like to at least understand the program flow before I start adding my changes...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So basically, you're saying this code has no purpose?
    It definitely has a purpose notifying some other thread that this part of the code was reached...

    Event is opened by its name - so it is originally created in some other part of the code...
    Here the event is signeled - so the wating function could exit...
    and when reset back to non-signaled state - so it could be used again
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vart View Post
    It definitely has a purpose notifying some other thread that this part of the code was reached...

    Event is opened by its name - so it is originally created in some other part of the code...
    Here the event is signeled - so the wating function could exit...
    and when reset back to non-signaled state - so it could be used again
    But does the call to SetEvent() block until something else happens, or does it return immediately? If it doesn't block, then I don't see how other threads could get notified about the event unless a context switch just happened to occur between the SetEvent() and ResetEvent() calls.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The effect is to release ALL waiting threads, and then reset the event. If you want to release only 1 thread, then use PulseEvent.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by abachler View Post
    The effect is to release ALL waiting threads, and then reset the event. If you want to release only 1 thread, then use PulseEvent.
    OK, but does SetEvent() guarantee that all threads will see the event in a signalled state before ResetEvent() sets it back to unsignalled? or do you need to call something like WaitForMultipleObjects() in between to make sure that all the threads heard the signal?

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    SetEvent will release all waiting threads.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    SetEvent only affects the synchronization object. It does not block. It's used to signal that an event has happened. WaitForSingleObject can be used to wait on a handle created by CreateEvent. As long as the object is non-signaled as ResetEvent, then WaitForSingleObject blocks. When SetEvent signals the object, WaitForSingleObject wakes the thread up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm still a bit confused.
    OK, let me put it this way: Lets say I'm running on single CPU machine, so only 1 thread can run at a time. I'm also running a million threads (just hypothetically of course) that are all waiting for an event.
    Now, if I call:
    Code:
    SetEvent( hEvent );  // Nothing in between these lines.
    ResetEvent( hEvent );
    You're saying that SetEvent() doesn't block, so it will set hEvent to a signalled state, then immediately afterwards ResetEvent() will set hEvent back to an unsignalled event.

    What I'm wondering is, how can all those other threads see that hEvent is signalled BEFORE it gets set back to unsignalled?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, technically, after the event is set, the system would check and release all locks waiting for that event to be set, so all those million threads would wake up and begin to cause mayhem as soon as you set it.
    When resetting it, the threads will fall back into deep slumber as soon as they wait for it to become signaled again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    As cutey pie said, SetEvent() is a system call, the system releases all waiting threads before returning. This doesnt mean they all execute immediately though, they are just scheduled for execution.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Moved to Windows programming.

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    This is all assuming that the event was created as a manual reset event.

    For manual reset events, here's some things you should keep in mind - say your thread(s) are looping on WaitForSingleObject(), then there is no telling how many loops will execute while the event is signaled (between the Set and Reset calls). Using the same loop construct, it's equally possible for any thread(s) to be in the middle of the loop (not inside a Wait function) during the entire duration the event is signaled - so that when it loops back around and calls a wait function, it blocks. In other words, those thread(s) never knew that the event was ever signaled.

    If it's actually an auto reset event, then the code you posted basically means "release one thread that may be waiting on the event, and just incase there wasn't a thread waiting on the event, reset it".

    gg

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    also, you might have better figuring out luck finding out the reasoning behind that snippet of code by simply going to the source - wherever CreateEvent and WaitForXXX are called. my guess is that the relevent code between was removed while the synchronization code remained (as a placeholder or simply because it was overlooked). who knows though, really.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed