How about:
Check the lock. If it is held, sleep for an interval that is shorter
than acceptable waiting time. If it is still held, sleep for twice as long.
Loop until you get the lock and do the work, or until you
you reach the limit for how much you can wait at this point and
dispatch to a thread instead.
This approach should be portable, don't wake up too often,
and don't waste the CPU. (And it won't go idle either, whoever
holds the lock will be running.)
Helge Hafting
-