Re: Lock convoys



chris noonan wrote:
Back in the topic "Lock Free -- where to start" (October 2005),
I gave a quantitative example of how using a lock in a
multithreaded application can increase the number of
context switches the system suffers eleven-fold.

My analysis was of the form:
"Every time a thread is caught holding the lock at the expiry of
its scheduling quantum, the following bad things happen ..."

At the time, I couldn't extend the example to an application
in which thread switching is mainly I/O driven. Besides, eleven-
fold is enough to affect performance, but not to destroy it.

Recently I stumbled across this excellent article which
presents a more powerul mechanism:
http://blogs.msdn.com/sloh/archive/2005/05/27/422605.aspx

The gist of it is that certain synchronisation objects impose
fairness, for example only allowing contending threads to take
a lock strictly in the order in which they blocked on it. Then
the argument becomes:
"If a thread is ever caught holding the lock at the expiry of
its scheduling quantum, even once, the following bad things
happen evermore ..."

AFAICS, if two threads are involved, normal service is likely
to be resumed at the end of the next quantum. If more threads
are involved, the lock convoy persists indefinitely. The cost
of the convoy is one thread switch per lock access, which
could easily mean thousands of switches per quantum
(before taking into account the cost of the switches
themselves).

The example in the article is for Windows CE critical section,
and the article hints that the way critsec works is subject to
change. Does anyone know which other OS'es are likely to
show this behaviour?


I think this is just the problem that if you have contention on
a lock, the suspend/resume overhead on waiting threads gets added
to the service time for a critical section so you get a hysteresis
effect if you graph performance of the critical section under
increasing and decreasing contention.

Adaptive mutexes do somewhat better than FIFO mutexes since they
allow "queue jumping" and successful queue jumpers don't add
suspend/resume overhead as long as the contention is not constant.
Other strategies to reduce overhead is to spin on the lock for a
a while if the lock owner is executing, the trade off being the
overhead of spinning vs. suspend/resume overhead not taken.
Another strategy is to boost the priority of threads holding the
lock to speed things up. Can also be used as a solution for
priority inversion.




--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software. .



Relevant Pages

  • Re: Tech: Whirlwind target bug-like oddity
    ... the game only spots a compass target if you do ... must be completed to light Lock... ... shows what I'd expect - 3 trough switches and ramp down. ...
    (rec.games.pinball)
  • Re: Lock convoys
    ... context switches the system suffers eleven-fold. ... "Every time a thread is caught holding the lock at the expiry of ... its scheduling quantum, the following bad things happen ..." ... thread/process that accepts messages from clients. ...
    (comp.programming.threads)
  • Re: Brewfest
    ... group with guildies, and we run it as many times as we're able. ... This is what I do as well, it's usually my lock, my GM's lock, another ... is done, the dual boxer switches the DK out for a rogue, and our pally ... just a matter of logging to alts and getting them summoned in. ...
    (alt.games.warcraft)
  • Re: Lock Free -- where to start
    ... I know IOCP sometimes schedules more). ... connection between IOCP and lock efficiency is. ... >>> switches before the original thread gets the processor ... you need the assumption that the system can get *no* useful work ...
    (comp.programming.threads)
  • Re: An E28 wiring question.
    ... reverse polarity to make the lock go in the opposite direction. ... lock solenoid are going to be the power that tells the solenoid to push ... The wires going to the lock switches might be grounded ... to tell the locking system what the key is doing. ...
    (alt.autos.bmw)