Patrick Collins
Patrick Collins

Reputation: 10584

Can I force a crash when I double-unlock a pthread_mutex_t?

Related to this question --- I'm debugging some code that unlocks a mutex twice and I'm trying to figure out exactly when it happens. I'm getting stack traces out of Helgrind, but I'd like the program to crash immediately when it performs the bad unlock.

I know the behavior of unlocking an already-unlocked mutex is undefined, and so nasal demons are an acceptable implementation --- I certainly can't force nasal demons to crash. But given the pthreads implementation in recent versions of glibc, is there a way to get this behavior reasonably reliably?

Upvotes: 1

Views: 514

Answers (1)

Kaz
Kaz

Reputation: 58598

Instead of obtaining a crash, what you can do is check the return value of pthread_mutex_unlock, and use an error-checking mutex (PTHREAD_MUTEX_ERRORCHECK mutex type). If you detect an error, call abort() or whatever.

Upvotes: 3

Related Questions