Nobody Is Blocked
This protocol replaces the second blocking acquire with a trylock, and it does remove deadlock: no thread ever holds one lock while waiting on another.
top:
pthread_mutex_lock(L1);
if (pthread_mutex_trylock(L2) != 0) {
pthread_mutex_unlock(L1);
goto top;
}
/* both held: do the work */
Thread 2 runs the same loop with L2 and L1 swapped. What can still go wrong?
Sign in to answer questions and track your progress
Sign In