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 11#ifndef ASIO_DETAIL_IMPL_POSIX_THREAD_IPP 12#define ASIO_DETAIL_IMPL_POSIX_THREAD_IPP 13 14 15#include "asio/detail/config.hpp" 16 17#if defined(ASIO_HAS_PTHREADS) 18 19#include "asio/detail/posix_thread.hpp" 20#include "asio/detail/throw_error.hpp" 21#include "asio/error.hpp" 22 23#include "asio/detail/push_options.hpp" 24 25namespace asio { 26namespace detail { 27 28posix_thread::~posix_thread() 29{ 30 if (!joined_) 31 ::pthread_detach(thread_); 32} 33 34void posix_thread::join() 35{ 36 if (!joined_) 37 { 38 ::pthread_join(thread_, 0); 39 joined_ = true; 40 } 41} 42 43void posix_thread::start_thread(func_base* arg) 44{ 45 int error = ::pthread_create(&thread_, 0, 46 asio_detail_posix_thread_function, arg); 47 if (error != 0) 48 { 49 delete arg; 50 asio::error_code ec(error, 51 asio::error::get_system_category()); 52 asio::detail::throw_error(ec, "thread"); 53 } 54} 55 56void* asio_detail_posix_thread_function(void* arg) 57{ 58 posix_thread::auto_func_base_ptr func = { 59 static_cast<posix_thread::func_base*>(arg) }; 60 func.ptr->run(); 61 return 0; 62} 63 64} // namespace detail 65} // namespace asio 66 67#include "asio/detail/pop_options.hpp" 68 69#endif // defined(ASIO_HAS_PTHREADS) 70 71#endif // ASIO_DETAIL_IMPL_POSIX_THREAD_IPP 72