5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
12 #define BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/config.hpp>
22 #include <boost/system/system_error.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #if defined(BOOST_HAS_PTHREADS)
27 #include <boost/asio/detail/push_options.hpp>
29 #include <boost/throw_exception.hpp>
31 #include <boost/asio/detail/pop_options.hpp>
33 #include <boost/asio/error.hpp>
34 #include <boost/asio/detail/noncopyable.hpp>
40 extern "C" void* asio_detail_posix_thread_function(void* arg
);
47 template <typename Function
>
48 posix_thread(Function f
)
51 std::auto_ptr
<func_base
> arg(new func
<Function
>(f
));
52 int error
= ::pthread_create(&thread_
, 0,
53 asio_detail_posix_thread_function
, arg
.get());
56 boost::system::system_error
e(
57 boost::system::error_code(error
,
58 boost::asio::error::get_system_category()),
60 boost::throw_exception(e
);
69 ::pthread_detach(thread_
);
72 // Wait for the thread to exit.
77 ::pthread_join(thread_
, 0);
83 friend void* asio_detail_posix_thread_function(void* arg
);
88 virtual ~func_base() {}
89 virtual void run() = 0;
92 template <typename Function
>
115 inline void* asio_detail_posix_thread_function(void* arg
)
117 std::auto_ptr
<posix_thread::func_base
> f(
118 static_cast<posix_thread::func_base
*>(arg
));
123 } // namespace detail
127 #endif // defined(BOOST_HAS_PTHREADS)
129 #include <boost/asio/detail/pop_options.hpp>
131 #endif // BOOST_ASIO_DETAIL_POSIX_THREAD_HPP