I messed a little bit with pthreads and needed an alternative to the C++11 function std::lock
(2 args are enough), and this is what I came up with:
void lock(pthread_mutex_t* m1, pthread_mutex_t* m2) {
while(1) {
pthread_mutex_lock(m1);
if(pthread_mutex_trylock(m2) == 0) { // if lock succesfull
break;
}
pthread_mutex_unlock(m1);
sched_yield();
}
}
It works fine so far. I am just not sure whether I missed some potential deadlock.
In addition, I am interested if I should do some more error checking. I am especially not that firm with C-style error checking.