Subj : Re: Recursive mutexes To : comp.programming.threads From : Chris Thomasson Date : Wed Mar 30 2005 01:42 pm > Any advantages if enforcing the if with "&& m_owner == thread_id" ? In any > case, > it should be expected the code not being ill behaved. Or I could add this > check and > display an error, right? Yes. You can display an error. I use a very simple mutex error checking algorithm for my AppCore library. It uses TLS so you don't have to worry about extra memory barriers which can hamper performance. However, you do have to check the performance of pthread_self(). Generally I try to minimize calls to pthread_self() by including a pointer to TLS in the synchronization object's interface. Here is a snippet of code that implements the simple error checking on a per-thread basis. Its fairly generic so you should be able to use its logic for your custom locks: /* recurse bucket */ typedef struct ac_thread_shared_recurse_ { void *lock; ac_intword_t count; } ac_thread_shared_recurse_t; /* thread local storage */ struct ac_thread_ { ... /* zero out array on init */ ac_thread_shared_recurse_t recurse[MAX_RECURSE_DEPTH]; /* init to -1 */ ac_intword_t recurse_index; ... }; /* api call parameters _this = pointer to TLS lock = pointer to synchronization object ac_sys_thread_recurse_whatever ( ac_thread_t *_this, void *lock ); return values: 0 = don't access sync object, recurse instead non-zero = need to access sync object */ /* You would call this function before _this trys to acquire the synchronization object */ int ac_sys_thread_recurse_inc ( ac_thread_t *_this, void *lock ) { ac_intword_t recurse = _this->recurse_index; if ( recurse > -1 && _this->recurse[recurse].lock == lock ) { ++_this->recurse[recurse].count; return 0; } return 1; } /* You would call this function after _this has acquired the synchronization object */ void ac_sys_thread_recurse_locked ( ac_thread_t *_this, void *lock ) { ac_intword_t recurse = ++_this->recurse_index; assert( ! _this->recurse[recurse].count ); _this->recurse[recurse].lock = lock; } /* You would call this function before _this releases the synchronization object */ int ac_sys_thread_recurse_dec ( ac_thread_t *_this, void *lock ) { ac_intword_t recurse = _this->recurse_index, count = _this->recurse[recurse].count; if ( _this->recurse[recurse].lock != lock ) { assert( _this->recurse[recurse].lock == lock ); return ac_sys_error( AC_ECORRUPTED ); } if ( count ) { _this->recurse[recurse].count = count - 1; return 0; } _this->recurse_index = recurse - 1; return 1; } -- http://appcore.home.comcast.net/ (portable lock-free data-structures) .