Thread yield after mutex unlock?
Is is sensible to unlock a mutex and immediately re-lock it? Consider this example:
pthread_mutex_lock(mutex);
while(1) {
/* Do work. */
pthread_mutex_unlock(mutex);
pthread_mutex_lock(mutex);
}
The intention here is to make other threads wait until we’ve finished doing work. Then we give them a chance to get in before we go round again and do more work. But can we guarantee that the scheduler will allow another thread to get in when we unlock a mutex? Is it possible that this thread will immediately re-lock the mutex, and never give other threads a chance to get in? Read the rest of this entry »