multithreading - How does the recursive(reentrant) mutex works? -
i read 2 articles @ http://preshing.com/20120305/implementing-a-recursive-mutex http://en.wikipedia.org/wiki/reentrant_mutex on recursive(reentrant) mutex, neither article made sense.
could explain how recursive(reentrant) mutex works ?
(i found little material explaining how recursive mutex works. if has link explanation, close question.)
thanks !
one simple way pair standard mutex following auxiliary information:
- a pointer thread owns mutex (or
null
if it's not acquired), and - a counter, 0.
you can acquire mutex in following way:
- if current mutex owner, increment counter , return immediately.
- if not, acquire mutex , set counter 0.
in other words, if own mutex already, increment counter indicating own more. if not, acquire mutex usual.
you can release mutex in following way:
- if counter nonzero, decrement counter , return immediately.
- otherwise, release mutex.
in order work, need able read counter , mutex owner in thread-safe way. can either having secondary mutex guard it, or marking counter / owner volatile
.
hope helps!
Comments
Post a Comment