posix_thread.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // posix_thread.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
  11. #define BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #if defined(BOOST_HAS_PTHREADS)
  21. #include <boost/asio/detail/push_options.hpp>
  22. #include <memory>
  23. #include <boost/throw_exception.hpp>
  24. #include <pthread.h>
  25. #include <boost/asio/detail/pop_options.hpp>
  26. #include <boost/asio/error.hpp>
  27. #include <boost/asio/detail/noncopyable.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. extern "C" void* asio_detail_posix_thread_function(void* arg);
  32. class posix_thread
  33. : private noncopyable
  34. {
  35. public:
  36. // Constructor.
  37. template <typename Function>
  38. posix_thread(Function f)
  39. : joined_(false)
  40. {
  41. std::auto_ptr<func_base> arg(new func<Function>(f));
  42. int error = ::pthread_create(&thread_, 0,
  43. asio_detail_posix_thread_function, arg.get());
  44. if (error != 0)
  45. {
  46. boost::system::system_error e(
  47. boost::system::error_code(error,
  48. boost::asio::error::get_system_category()),
  49. "thread");
  50. boost::throw_exception(e);
  51. }
  52. arg.release();
  53. }
  54. // Destructor.
  55. ~posix_thread()
  56. {
  57. if (!joined_)
  58. ::pthread_detach(thread_);
  59. }
  60. // Wait for the thread to exit.
  61. void join()
  62. {
  63. if (!joined_)
  64. {
  65. ::pthread_join(thread_, 0);
  66. joined_ = true;
  67. }
  68. }
  69. private:
  70. friend void* asio_detail_posix_thread_function(void* arg);
  71. class func_base
  72. {
  73. public:
  74. virtual ~func_base() {}
  75. virtual void run() = 0;
  76. };
  77. template <typename Function>
  78. class func
  79. : public func_base
  80. {
  81. public:
  82. func(Function f)
  83. : f_(f)
  84. {
  85. }
  86. virtual void run()
  87. {
  88. f_();
  89. }
  90. private:
  91. Function f_;
  92. };
  93. ::pthread_t thread_;
  94. bool joined_;
  95. };
  96. inline void* asio_detail_posix_thread_function(void* arg)
  97. {
  98. std::auto_ptr<posix_thread::func_base> f(
  99. static_cast<posix_thread::func_base*>(arg));
  100. f->run();
  101. return 0;
  102. }
  103. } // namespace detail
  104. } // namespace asio
  105. } // namespace boost
  106. #endif // defined(BOOST_HAS_PTHREADS)
  107. #include <boost/asio/detail/pop_options.hpp>
  108. #endif // BOOST_ASIO_DETAIL_POSIX_THREAD_HPP