thread_guard.hpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/config/abi_prefix.hpp>
  13. namespace boost
  14. {
  15. /**
  16. * Non-copyable RAII scoped thread guard joiner which join the thread if joinable when destroyed.
  17. */
  18. template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
  19. class thread_guard
  20. {
  21. Thread& t_;
  22. public:
  23. BOOST_THREAD_NO_COPYABLE( thread_guard)
  24. explicit thread_guard(Thread& t) :
  25. t_(t)
  26. {
  27. }
  28. ~thread_guard()
  29. {
  30. CallableThread on_destructor;
  31. on_destructor(t_);
  32. }
  33. };
  34. }
  35. #include <boost/config/abi_suffix.hpp>
  36. #endif