[concurrency-interest] Low cpu usage with fair ReentrantLock
dl at cs.oswego.edu
Mon May 10 12:48:47 EDT 2010
On 05/10/10 10:30, Andrew Haley wrote:
> Does anyone have any idea why a fair ReentrantLock should result in
> such low CPU usage? This pattern of usage has, apparently, been
> observed in more realistic workloads, from which this test case has
> been extracted.
Because blocking and unblocking a thread can be 100X more
expensive than just performing a CAS.
Under fair locks, even if there are on average plenty of
"spaces" where the lock is available, a few collisions
will tend to lead to sustained phases where the lock
is awaiting the first waiter to wake up, causing others
to block until it does. So the entire program can
only run at the rate it takes to block+unblock a thread
(a form of "convoying").
For non-fair locks, other threads are allowed to "barge"
in during these periods where the lock is not actually
held, and grab the lock before head of queue wakes up.
This tends to happen a lot, making them much faster.
More information about the Concurrency-interest