test_thread_exit.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // (C) Copyright 2009 Anthony Williams
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/thread/thread_only.hpp>
  7. #include <boost/thread/mutex.hpp>
  8. #include <boost/thread/condition.hpp>
  9. #include <boost/thread/future.hpp>
  10. #include <utility>
  11. #include <memory>
  12. #include <string>
  13. #define BOOST_TEST_MODULE Boost.Threads: thread exit test suite
  14. #include <boost/test/unit_test.hpp>
  15. boost::thread::id exit_func_thread_id;
  16. void exit_func()
  17. {
  18. exit_func_thread_id=boost::this_thread::get_id();
  19. }
  20. void tf1()
  21. {
  22. boost::this_thread::at_thread_exit(exit_func);
  23. BOOST_CHECK(exit_func_thread_id!=boost::this_thread::get_id());
  24. }
  25. BOOST_AUTO_TEST_CASE(test_thread_exit_func_runs_when_thread_exits)
  26. {
  27. exit_func_thread_id=boost::thread::id();
  28. boost::thread t(&tf1);
  29. boost::thread::id const t_id=t.get_id();
  30. t.join();
  31. BOOST_CHECK(exit_func_thread_id==t_id);
  32. }
  33. struct fo
  34. {
  35. void operator()()
  36. {
  37. exit_func_thread_id=boost::this_thread::get_id();
  38. }
  39. };
  40. void tf2()
  41. {
  42. boost::this_thread::at_thread_exit(fo());
  43. BOOST_CHECK(exit_func_thread_id!=boost::this_thread::get_id());
  44. }
  45. BOOST_AUTO_TEST_CASE(test_can_use_function_object_for_exit_func)
  46. {
  47. exit_func_thread_id=boost::thread::id();
  48. boost::thread t(tf2);
  49. boost::thread::id const t_id=t.get_id();
  50. t.join();
  51. BOOST_CHECK(exit_func_thread_id==t_id);
  52. }