[Thread]Deadlock using shared_mutex with unique_lock
Hi everyone, I am trying to use shared_mutex with unique_lock but my program blocks. I am using boost 1.35. My compiler is MSVC 8.0.50727.762 and my OS is Windows 2000. Here is a minimal code to reproduce the problem: #include "boost/thread.hpp" #include "boost/thread/shared_mutex.hpp" #include "boost/bind.hpp" class Executor { public: void run() { boost::unique_lockboost::shared_mutex lock(theMutex); boost::thread::sleep(boost::get_system_time() + boost::posix_time::millisec(30)); } private: boost::shared_mutex theMutex; }; void main() { Executor ex; boost::thread_group group; for(int i=0; i<200; i++) { group.create_thread(boost::bind(&Executor::run, &ex)); } group.join_all(); } The program blocks during the call of group.join_all(). According to the Boost.Thread documentation, unique_lockboost::shared_mutex lock(theMutex) acquires exclusive ownership of theMutex. Therefore, I expect that the behaviour of boost::unique_lockboost::shared_mutex lock(theMutex); is the same as if a boost::mutex is used. Is my assumption correct or am I missing something? Thank you for any help you can provide, Stanislav
Stanislav Stoyanov
I am trying to use shared_mutex with unique_lock but my program blocks. I am using boost 1.35. My compiler is MSVC 8.0.50727.762 and my OS is Windows 2000. Here is a minimal code to reproduce the problem:
#include "boost/thread.hpp" #include "boost/thread/shared_mutex.hpp" #include "boost/bind.hpp"
class Executor { public: void run() { boost::unique_lockboost::shared_mutex lock(theMutex); boost::thread::sleep(boost::get_system_time() + boost::posix_time::millisec(30)); } private: boost::shared_mutex theMutex; };
void main() { Executor ex; boost::thread_group group; for(int i=0; i<200; i++) { group.create_thread(boost::bind(&Executor::run, &ex)); } group.join_all(); }
The program blocks during the call of group.join_all(). According to the Boost.Thread documentation, unique_lockboost::shared_mutex lock(theMutex) acquires exclusive ownership of theMutex. Therefore, I expect that the behaviour of boost::unique_lockboost::shared_mutex lock(theMutex); is the same as if a boost::mutex is used. Is my assumption correct or am I missing something?
Your code looks right at first glance, and I agree there's a problem. I'm looking into it. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
Stanislav Stoyanov
I am trying to use shared_mutex with unique_lock but my program blocks. I am using boost 1.35. My compiler is MSVC 8.0.50727.762 and my OS is Windows 2000. Here is a minimal code to reproduce the problem:
Your problem is here:
for(int i=0; i<200; i++) { group.create_thread(boost::bind(&Executor::run, &ex)); }
You've overrun the internal limits of boost::shared_mutex on Windows: there can be no more than 127 threads waiting for an exclusive lock. Unfortunately, the library doesn't detect this and corrupts the mutex if the count overflows. The equivalent limit for shared_locks is 2047. If you reduce the upper limit of your loop to 128 then your code should run fine. I've created trac ticket #2293 to remind me to add the error checks. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
participants (2)
-
Anthony Williams
-
Stanislav Stoyanov