Boost logo

Boost :

From: Maciej Sobczak (prog_at_[hidden])
Date: 2003-12-17 16:02:44


Hi,

Pete wrote:

> To me, the Linux behaviour makes perfect sense. Why doesn't it behave in the
> same way under Windows?

Both make perfect sense.

You should certainly read:

http://www.boost.org/libs/thread/doc/mutex_concept.html

(the Unspecified Locking Strategy is most relevant here)

In short: boost::mutex does not specify what will happen when *the same*
thread will try to lock the mutex the second time - according to
documentation referenced above it is undefined behavior.

The undefined behavior is the simplest thing here and just boils down to
what the native implementation provides. It just happens that Windows'
"Critical Section" is a recursive mutex (the same thread can lock twice)
whereas the pthread_mutex on Linux is not and therefore deadlocks.
Both behaviors are arbitrary choices of Windows/Linux implementers. It
could as well dump core.

All behaviors are OK as far as the documentation says "undefined
behavior". Just do not rely on it being portable.

If you really (portably) want the recursive mutex, use...
boost::recursive_mutex.

If you really (portably) want to deadlock, use... two mutexes. ;-)

-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk