long wait times should be avoided. However, think about how short wait
times should gain the lock in step 0 more often than not.
The slow path fallback is step 2:
_____________________________________
for (;;)
{
0: if (try_lock()) return;
1: if (! try_something_else())
{
// possible nesting opportunity...
2: lock();
3: return;
}
}
_____________________________________
Not sure about deadlocks yet, should not be a problem if we know some
things about the nature of step 1. This can be modeled wrt making step 1
random. Sometimes it has some work to do, other times it does not. When
not, it falls back to the slow path in step 2.
There should be a limit on the for (;;) to prevent infinite loop wrt
always having other work to do, and always failing the try lock.
So, perhaps something like:
_______________________________________
for (unsigned int i = 0; i < 42; ++i)
{
if (try_lock()) return;
if (! try_something_else())
{
break;
}
}
lock();
return;
_______________________________________
Trying to think of a contrived usage case here.
;^)