1 // Copyright (C) 2010 Vicente Botet 2 // 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) 5 6 #include <iostream> 7 #include <boost/thread.hpp> 8 #include <boost/thread/thread_only.hpp> 9 #include <boost/optional.hpp> 10 #include <boost/detail/lightweight_test.hpp> 11 12 using namespace boost; 13 using namespace boost::chrono; 14 15 struct dummy_class_tracks_deletions 16 { 17 static unsigned deletions; 18 dummy_class_tracks_deletionsdummy_class_tracks_deletions19 dummy_class_tracks_deletions() 20 { 21 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 22 } ~dummy_class_tracks_deletionsdummy_class_tracks_deletions23 ~dummy_class_tracks_deletions() 24 { 25 ++deletions; 26 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 27 } 28 29 }; 30 unsigned dummy_class_tracks_deletions::deletions=0; 31 32 33 optional<thread_specific_ptr<dummy_class_tracks_deletions> > optr; 34 //struct X 35 //{ 36 // thread_specific_ptr<int> f; 37 //} sptr; 38 other_thread()39void other_thread() 40 { 41 42 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 43 optr = none; 44 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 45 optr = in_place(); 46 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 47 BOOST_TEST(optr->get() == 0); 48 this_thread::sleep(posix_time::seconds(5)); 49 BOOST_TEST(optr->get() == 0); 50 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 51 52 } 53 main()54int main() 55 { 56 57 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 58 dummy_class_tracks_deletions * pi = new dummy_class_tracks_deletions; 59 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 60 optr = in_place(); 61 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 62 optr->reset(pi); 63 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 64 BOOST_TEST(optr->get() == pi); 65 thread t1(bind(&other_thread)); 66 this_thread::sleep(posix_time::seconds(5)); 67 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 68 BOOST_TEST(optr->get() == pi); 69 std::cout << __FILE__ << ":" << __LINE__ << boost::this_thread::get_id() << std::endl; 70 t1.join(); 71 return boost::report_errors(); 72 73 } 74