]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/test_7755.cpp
1 // Copyright (C) 2013 Vicente Botet
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ////////////////////////////////////////////
8 //#define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
10 #include <boost/thread/thread_only.hpp>
11 #include <boost/thread/shared_mutex.hpp>
12 // shared_mutex_deadlock.cpp : Defines the entry point for the console application.
16 boost::shared_mutex mutex
;
23 if (mutex
.timed_lock(boost::posix_time::milliseconds(500)))
25 std::cout
<< "Unique lock acquired" << std::endl
26 << "Test successful" << std::endl
;
33 std::cout
<< "Test failed. App is deadlocked" << std::endl
;
36 boost::this_thread::sleep(boost::posix_time::seconds(1));
42 boost::shared_lock
<boost::shared_mutex
> lock (mutex
);
43 std::cout
<< "Shared lock acquired" << std::endl
44 << "Test successful" << std::endl
;
47 void thread3_func_workaround()
51 if (mutex
.timed_lock_shared(boost::posix_time::milliseconds(200)))
53 std::cout
<< "Shared lock acquired" << std::endl
54 << "Test successful" << std::endl
;
55 mutex
.unlock_shared();
58 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
64 std::cout
<< "Starting" << std::endl
;
67 boost::shared_lock
<boost::shared_mutex
> lock (mutex
);
70 boost::thread
thread2(&thread2_func
);
73 boost::this_thread::sleep(boost::posix_time::milliseconds(10));
75 std::cout
<< "Thread#2 is waiting" << std::endl
;
78 boost::thread
thread3(&thread3_func
);
79 //boost::thread thread3(&thread3_func_workaround);
81 std::cout
<< "Thread#3 is started and blocked. It never will waked" << std::endl
;
83 thread3
.join(); // will never return
85 lock
.unlock(); // release shared ownership. thread#2 will take unique ownership
89 std::cout
<< "Finished" << std::endl
;