thread_guard.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2009-2012 Anthony Williams
  5. // (C) Copyright 2012 Vicente J. Botet Escriba
  6. // Based on the Anthony's idea of thread_joiner in CCiA
  7. #ifndef BOOST_THREAD_THREAD_GUARD_HPP
  8. #define BOOST_THREAD_THREAD_GUARD_HPP
  9. #include <boost/thread/detail/delete.hpp>
  10. #include <boost/thread/detail/move.hpp>
  11. #include <boost/thread/thread_functors.hpp>
  12. #include <boost/thread/detail/thread_interruption.hpp>
  13. #include <boost/config/abi_prefix.hpp>
  14. namespace boost
  15. {
  16. /**
  17. * Non-copyable RAII scoped thread guard joiner which join the thread if joinable when destroyed.
  18. */
  19. template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
  20. class thread_guard
  21. {
  22. Thread& t_;
  23. public:
  24. BOOST_THREAD_NO_COPYABLE( thread_guard)
  25. explicit thread_guard(Thread& t) :
  26. t_(t)
  27. {
  28. }
  29. ~thread_guard()
  30. {
  31. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  32. // exceptions from a destructor call std::terminate
  33. boost::this_thread::disable_interruption do_not_disturb;
  34. #endif
  35. CallableThread on_destructor;
  36. on_destructor(t_);
  37. }
  38. };
  39. }
  40. #include <boost/config/abi_suffix.hpp>
  41. #endif