❔ Autoresetevent
Hello there, does the state of the Set() method of the object persists until another thread performs a WaitOne() on it?
Like if it happens that a manager thread do a Set() and the worker thread didn't arrive to the WaitOne() yet, will the state persist until the worker thread arrive and change the state?
4 Replies
Set() will keep its state open (allowing a thread that is calling WaitOne() to run) even though there is no thread arrived at WaitOne() line yet, until there is a thread calling WaitOne(), and when it happens, it will set the Autoresetevent state to close (block waiting thread that is waiting on WaitOne()).
this article written by Joseph Albahari has great explanation about AutoResetEvent
https://www.albahari.com/threading/part2.aspx#_AutoResetEvent
If Set is called when no thread is waiting, the handle stays open for as long as it takes until some thread calls WaitOne. This behavior helps avoid a race between a thread heading for the turnstile, and a thread inserting a ticket (“Oops, inserted the ticket a microsecond too soon, bad luck, now you’ll have to wait indefinitely!”). However, calling Set repeatedly on a turnstile at which no one is waiting doesn’t allow a whole party through when they arrive: only the next single person is let through and the extra tickets are “wasted.”
Perfectly fine, since my mechanism is dependent on a bool field and i thought that implementing a mutex for the bool field aswell as the rest of the other synchronizations would be excessive
Thank you
you welcome
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.