Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you call EventWaitHandle.Set() X times, will it signal X threads if X the threads haven't started yet?

In other words I have a thread that starts and calls waitHandle.Set() multiple times. Then I start several threads and each is waiting on that WaitHandle. Will X of the them be signaled where X is the number of times the original thread called waitHandle.Set()?

Is there a structure that supports what I'm trying to accomplish more closely?

like image 650
Orion Adrian Avatar asked May 05 '09 20:05

Orion Adrian


2 Answers

Maybe, but probably not.

An event can only be set or unset, if there is nothing to unset the event then repeated calls to Set will not change its state, unless it is an auto-reset event and there is at least one thread waiting on it. With a manual reset event any threads waiting (or starting to wait) will be released until something unsets the event.

I think you will actually want a semaphore, which does have a count and supports setting n times to release n threads (even if some of those threads only start waiting after some of the calls to set).

like image 73
Richard Avatar answered Oct 19 '22 16:10

Richard


It depends on the EventResetMode. If it is set to EventResetMode.AutoReset it will only release one thread. If it is 'EventResetMode.ManualReset' however it will release all threads blocked on the event.

Semaphore.Release(Int)

Where Int is the count would be one way to do this.

like image 32
Matt Davison Avatar answered Oct 19 '22 17:10

Matt Davison