Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread]Is shared_mutex recursive with respect to shared locks?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-12-29 13:59:19


AMDG

Ken Savela wrote:
> In other words, should the following code result in a deadlock?
>
>
>
> #include <iostream>
>
> #include <boost/thread.hpp>
>
> int main(int argc, char* argv[])
> {
> boost::shared_mutex smx;
>
> boost::upgrade_lock<boost::shared_mutex> lock_one(smx);
> std::cout << "Locked once." << std::endl;
> std::cout.flush();
> boost::upgrade_lock<boost::shared_mutex> lock_two(smx);
> std::cout << "Locked again." << std::endl;
> std::cout.flush();
> boost::upgrade_to_unique_lock<boost::shared_mutex> lock_three(lock_two);
> std::cout << "Write lock." << std::endl;
>
> return 0;
> }
>
>
>
> This example illustrates the problem I'm having in my app -- I have a
> situation where a single thread may end up performing more than a single
> shared lock. When it does so, it hangs on the nested upgrade_lock. Is this
> the expected behavior?
>

Yes this is the expected behavior. When you try to upgrade the second
shared lock it blocks until the other shared_lock is released. i.e. never.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net