Domanda

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?

È stato utile?

Soluzione

There is some overlap in how they can be used, but there are also some unique features to both:

  1. Critical sections cannot be used across processes, whereas events can.
  2. A single manual-reset event can be used to release multiple threads at once. A critical section cannot.
  3. Events are compatible with WaitForSingleObject() et al, whereas critical sections aren't.
  4. 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?

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top