Will mutexes be broken?

I'm doing it wrong, huh?

... if( you_think_youre_genius ) goto goto_sucks: ... pthread_mutex_lock(&mutex); do_stuff(); goto_sucks: do_other_stuff(); pthread_mutex_unlock(&mutex); 
+7
source share
3 answers

Yes, goto is a direct jmp down at the binary level, so any function calls between goto and the label will be skipped, period.

+12
source

A mutex is acquired inside the pthread_mutex_lock function. If you go through a function call, you will not acquire a mutex. If you try to lock the mutex twice, you might be stuck. If you try to unlock mutexes that you are not, you can greatly disrupt the work.

+5
source

If the condition is true, do_other_stuff will be called without locking the mutex, and then the mutex will be released without locking it. Bad mistake!

Just without goto

 if( you_think_youre_genius ) { pthread_mutex_lock(&mutex); } else { ... pthread_mutex_lock(&mutex); //Assumming no expetion thrown do_stuff(); } do_other_stuff(); pthread_mutex_unlock(&mutex); 
+1
source

All Articles