Hello,
I have come to a more interresting subject, it's the
exponential backoff mechanism that we use inside the Spinlock,
this backoff mechanism lower the contention, but i was thinking more
about it, and what does we mean by lower the contention ?
when you are spining inside a Spinlock you are testing also
for a flag/variable, but when you test for this variable to
equal for example 1 to be able to enter the locked region
you are generating cache-coherency traffic, cause when a thread
leaves a locked region it will set the flag to 1 for example,
but when the thread sets to 1 the variable , the variable is not
in the L2 cache of others threads, so the variable must be
transfered to the other threads and this is costly/expensive
and this generate a lot of cache-coherence traffic, but
the problem with the Spinlock is that when a lot of cache-coherence
is generated this will slow the threads that enters the Spinlock
and slows the threads globally in your computer, so this will slow the
threads , now there is still a problem if we use a Sleep() function
inside the backoff mechanism this will slow a lot the threads, so as i
told you before the solution is to use the PAUSE asm instruction inside
the backoff mechanism followed by a sleep(0), but about the lockfree
queues , even though a lot cache-coherence traffic is generated by the
threads when there is high contention , this will not slow the threads
inside the CAS , as in the case of the Spinlock, cause the CAS uses a
LOCK asm instruction that lock the Bus, so this is better than the
Spinlock, but even though it will not slow the thread inside the CAS
you have to use a PAUSE instruction inside an exponential backoff
mechanism to not slow globally the other threads and applications on
your computer when there is high contention.
Thank you,
Amine Moulay Ramdane,