2

Spin locks (busy waiting locks) are more efficient than mutex sleep locks for very short critical sections. Suppose that the context switch time for a system (the time it takes to save the current process and load the next) is time T. How long can a critical section be before it is more efficient to use a mutex sleep lock rather than a spin lock?

GoZoner
  • 67,920
  • 20
  • 95
  • 145
sunil kalluri
  • 21
  • 1
  • 3

1 Answers1

0

It depends on the specific case. How much cpu time are you willing to burn on spinning to wake up faster?

Intel once said 20 cycles. But this was very long ago in the days on many more threads than cores.

If your waiting thread is very critical to you, it'd have it's own dedicated core and you would not care to spin forever for the benefit of the ultimate fastest wake up.

If it's less critical than that, and the core is shared with other threads, you may want to give the cpu time to some other thread. If you don't do that, the OS will eventually do that for you, but this is less than optimal, obviously.

Bottom line - test and see the differences in performance and then re-iterate, re-test, etc.

BitWhistler
  • 1,439
  • 8
  • 12