Are mutexes really slower?

前端 未结 5 2069
梦毁少年i
梦毁少年i 2020-12-24 07:38

I have read so many times, here and everywhere on the net, that mutexes are slower than critical section/semaphores/insert-your-preferred-synchronisation-method-here. but i

5条回答
  •  梦毁少年i
    2020-12-24 08:15

    Yes, critical sections are more efficient. For a very good explanation, get "Concurrent Programming on Windows".

    In a nutshell: a mutex is a kernel object, so there is always a context switch when you acquire one, even if "free". A critical section can be acquired without a context switch in that case, and (on an multicore/processor machine) it will even spin a few cycles if it's blocked to prevent the expensive context switch.

提交回复
热议问题