Why is the throughput of the MCS lock poor when the number of threads is greater than the number of logical cpus

StackOverflow https://stackoverflow.com//questions/20036216

Question

Why is the throughput of the mcs lock poor when the number of threads is greater than the number of logical cpus. Could it be because of increased contention for a places on cpu which leads to a lot of threads being pre-empted?

Was it helpful?

Solution

I am not 100% on this, but the Microsoft library gives this definition of the Sleep() function:

After the sleep interval has passed, the thread is ready to run. If you specify 0 >milliseconds, the thread will relinquish the remainder of its time slice but remain >ready. Note that a ready thread is not guaranteed to run immediately. Consequently, the >thread may not run until some time after the sleep interval elapses.

In my experience, if I use an MCS lock to, lets say, update a data structure and the number of threads I run it on is 16 the drop off (excluding the massive drop off from 1 - 2 threads) from 8 to 16 threads (assuming you are just doubling the number of threads) is quite large. Throughput drops to about a third after one thread and then slowly decreased to as the number of threads being used approaches the number of CPUs. Obviously if you are using a lock the more threads that are trying to acquire the lock you will have more cache cache coherency work for the CPU's to do.

If you use any atomic instructions (again assuming you are) the more threads you add the slower this will become.

"I don't think the problem is that atomic operations will take longer themselves; the real problem might be that an atomic operation might block bus operations on other processors (even if they perform non-atomic operations)."

This was taken from another member of stackoverflow about similar issue. Couple that with the fact that a thread may or may not sleep, even with the use of Sleep(), and may or may not wake immediately this could cause a serious loss in throughput. You also have the increased bus traffic to deal with...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top