posix_thread.ipp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // detail/impl/posix_thread.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_DETAIL_IMPL_POSIX_THREAD_IPP
  11. #define ASIO_DETAIL_IMPL_POSIX_THREAD_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_PTHREADS)
  17. #include "asio/detail/posix_thread.hpp"
  18. #include "asio/detail/throw_error.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. posix_thread::~posix_thread()
  24. {
  25. if (!joined_)
  26. ::pthread_detach(thread_);
  27. }
  28. void posix_thread::join()
  29. {
  30. if (!joined_)
  31. {
  32. ::pthread_join(thread_, 0);
  33. joined_ = true;
  34. }
  35. }
  36. void posix_thread::start_thread(func_base* arg)
  37. {
  38. int error = ::pthread_create(&thread_, 0,
  39. asio_detail_posix_thread_function, arg);
  40. if (error != 0)
  41. {
  42. delete arg;
  43. asio::error_code ec(error,
  44. asio::error::get_system_category());
  45. asio::detail::throw_error(ec, "thread");
  46. }
  47. }
  48. void* asio_detail_posix_thread_function(void* arg)
  49. {
  50. posix_thread::auto_func_base_ptr func = {
  51. static_cast<posix_thread::func_base*>(arg) };
  52. func.ptr->run();
  53. return 0;
  54. }
  55. } // namespace detail
  56. } // namespace asio
  57. #include "asio/detail/pop_options.hpp"
  58. #endif // defined(ASIO_HAS_PTHREADS)
  59. #endif // ASIO_DETAIL_IMPL_POSIX_THREAD_IPP