If, for instance only 1 thread can access a Critical Section
at a time, why do we need Events
to synch 2 threads to read/write through it?
Asked
Active
Viewed 72 times
1

George Irimiciuc
- 4,573
- 8
- 44
- 88
2 Answers
1
There is some overlap in how they can be used, but there are also some unique features to both:
- Critical sections cannot be used across processes, whereas events can.
- A single manual-reset event can be used to release multiple threads at once. A critical section cannot.
- Events are compatible with
WaitForSingleObject()
et al, whereas critical sections aren't. - A thread can wait on multiple events with
WaitForMultipleObjects[Ex]()
, but it can only wait for a single critical section (using a different API).
and so on.
They are not really in direct competition; it's best to think of them as being complementary to each other.
Mutexes are somewhere in the middle. For further discussion, see What is the difference between mutex and critical section?
-
I know they are complementary to each other, but I want to understand this specific thing. Why do we need events for CS? Isn't CS synching the threads already? – George Irimiciuc Mar 29 '14 at 20:18
-
@GeorgeIrimiciuc: Well, you are asking a specific question without giving us any context. This makes it really hard to answer. – NPE Mar 29 '14 at 20:19
-
Hm, the context would be two threads using a CS to read and write alternatively. CS already blocks them alternatively, so why do we need Events for full synch? – George Irimiciuc Mar 29 '14 at 20:20
-
@GeorgeIrimiciuc Events allow you to block and wait until something happens, something a critical section does not. Often one waits for a shared resource that must be protected with e.g. a critical section. – nos Mar 29 '14 at 22:02
0
Events have a SetEvent function which lets it be used as a signal. When one thread has finished reading a file or filling a buffer, for example, it can SetEvent to get a different thread started on the next processing step for that data.

ScottMcP-MVP
- 10,337
- 2
- 15
- 15