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 7 #include <boost/thread/thread_only.hpp> 8 #include <boost/thread/mutex.hpp> 9 #include <boost/thread/condition.hpp> 10 #include <boost/thread/future.hpp> 11 #include <utility> 12 #include <memory> 13 #include <string> 14 15 #define BOOST_TEST_MODULE Boost.Threads: thread exit test suite 16 17 #include <boost/test/unit_test.hpp> 18 19 boost::thread::id exit_func_thread_id; 20 exit_func()21void exit_func() 22 { 23 exit_func_thread_id=boost::this_thread::get_id(); 24 } 25 tf1()26void tf1() 27 { 28 boost::this_thread::at_thread_exit(exit_func); 29 BOOST_CHECK(exit_func_thread_id!=boost::this_thread::get_id()); 30 } 31 BOOST_AUTO_TEST_CASE(test_thread_exit_func_runs_when_thread_exits)32BOOST_AUTO_TEST_CASE(test_thread_exit_func_runs_when_thread_exits) 33 { 34 exit_func_thread_id=boost::thread::id(); 35 boost::thread t(&tf1); 36 boost::thread::id const t_id=t.get_id(); 37 t.join(); 38 BOOST_CHECK(exit_func_thread_id==t_id); 39 } 40 41 struct fo 42 { operator ()fo43 void operator()() 44 { 45 exit_func_thread_id=boost::this_thread::get_id(); 46 } 47 }; 48 tf2()49void tf2() 50 { 51 boost::this_thread::at_thread_exit(fo()); 52 BOOST_CHECK(exit_func_thread_id!=boost::this_thread::get_id()); 53 } 54 55 BOOST_AUTO_TEST_CASE(test_can_use_function_object_for_exit_func)56BOOST_AUTO_TEST_CASE(test_can_use_function_object_for_exit_func) 57 { 58 exit_func_thread_id=boost::thread::id(); 59 boost::thread t(tf2); 60 boost::thread::id const t_id=t.get_id(); 61 t.join(); 62 BOOST_CHECK(exit_func_thread_id==t_id); 63 } 64