What I understand is that both WaitForMultipleObjects and CRITICAL_SECTION are meant to wait for threads to complete. And both of them being described as process and thread synchronization mechanisms between threads. Can they be used interchangeably if they are meant to achieve the same goal? If not then what is the difference between them?
What I understand is that both WaitForMultipleObjects and CRITICAL_SECTION are meant to wait for
Share
They are not interchangeable and serve different purposes.
A critical section is a mutex. Blocks of code wrapped in a critical can be entered by one thread at a time. This is also known as serialization because protected blocks are executed serially.
The
WaitForMultipleObjectsfunction and its various relatives are used to block until a synchronisation object is signaled. This could be an event becoming signaled, a thread completing, a process completing, a mutex becoming available, etc.Typically wait functions are used to ensure dependencies are correctly handled. For example, if a calculation can only proceed when other calculations have completed, a wait function will be used to block until those other calculations have completed. Using a proper wait function rather than a busy spin look avoids wasting clock cycles.