Boost logo

Boost :

From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2007-06-26 16:08:21


On 6/26/07, John Zorko <jmzorko_at_[hidden]> wrote:
>
> Hello, all ...
> [...]
> boost::mutex g_Mutex;
>
> void f1()
> {
> boost::mutex::scoped_lock lock( g_Mutex );
>
> std::cout << "1\n";
> }
>
> int main(int argc, char* argv[])
> {
> boost::mutex::scoped_lock lock( g_Mutex );
>
> std::cout << "main\n";
>
> f1();
>
> return 0;
> }
>
> ... under Windows XP SP2 (using VC 8.0), the output is this:
>
> main
> 1
>
> ... however, under Mac OSX 10.4.10 (using Apple's gcc4.0.1), the
> output is:
>
> main
> (program hangs)
>

Trying to lock a mutex from a thread that already owns it in
Boost.Threads is undefined behavior. Under Windows (because of the
underlying implementation) it happens to work, but it deadlocks on
other platforms.

If you really want a recursive mutex you should use boost.thread
recursive_mutex instead of plain mutexes. Anyways, consider that
recursive mutexes are generally "Considered Harmful" [1]

Also, probably this question belongs to the boost.users mailing list.

HTH,

gpd

[1]: http://zaval.org/resources/library/butenhof1.html


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