test_3837.cpp 2.3 KB

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