-3

I read about event, which allow me to wait for other thread: AutoResetEvent and ManualResetEvent.

What are the differences between these two classes? Which class is better for a highly concurrent program?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jacek
  • 11,661
  • 23
  • 69
  • 123
  • 5
    Have you read the documentation? It very clearly explains the differences. Neither is better or worse than the other, they are meant for different things. – Cory Nelson Mar 14 '13 at 17:34
  • 3
    The question is too vague to answer. Regarding your first question: What part of the documentation is unclear? Regarding your second question: if I handed you two screwdrivers and asked you which one was "better" I hope you'd say "the one that fits the screw you need to drive". – Eric Lippert Mar 14 '13 at 17:40
  • _"the one that fits the screw you need to drive"_ - that's pure gold! :) – Mike Dinescu Mar 14 '13 at 17:43

1 Answers1

4

The difference is in what happens when the event is signaled (set).

  • the manual-reset event will stay signaled until your explicitly reset it again
  • the auto-reset event will automatically get reset (unsignaled) once the first thread waiting for it gets awaken

In general I find it easier to work with manual reset events because in most cases it is a bit more straight-forward to determine the state of the event at any given time.

That said there are cases when the behavior of the auto-reset event lends itself better to achieving synchronization because you are guaranteed that only one of the waiting threads will be signaled. So if you have a producer/multiple-consumers scenario where any, but only, one consumer should be signaled you should consider the auto-reset event.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151