File indexing completed on 2026-08-31 08:41:39
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
0012 #define BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019
0020 #if defined(BOOST_ASIO_HAS_PTHREADS)
0021
0022 #include <cstddef>
0023 #include <pthread.h>
0024 #include <boost/asio/detail/memory.hpp>
0025
0026 #include <boost/asio/detail/push_options.hpp>
0027
0028 namespace boost {
0029 namespace asio {
0030 namespace detail {
0031
0032 extern "C"
0033 {
0034 BOOST_ASIO_DECL void* boost_asio_detail_posix_thread_function(void* arg);
0035 }
0036
0037 class posix_thread
0038 {
0039 public:
0040
0041 posix_thread() noexcept
0042 : arg_(0)
0043 {
0044 }
0045
0046
0047 template <typename Function>
0048 posix_thread(Function f, unsigned int = 0)
0049 : posix_thread(std::allocator_arg, std::allocator<void>(), f)
0050 {
0051 }
0052
0053
0054 template <typename Allocator, typename Function>
0055 posix_thread(allocator_arg_t, const Allocator& a,
0056 Function f, unsigned int = 0)
0057 : arg_(start_thread(allocate_object<func<Function, Allocator>>(a, f, a)))
0058 {
0059 }
0060
0061
0062 posix_thread(posix_thread&& other) noexcept
0063 : arg_(other.arg_)
0064 {
0065 other.arg_ = 0;
0066 }
0067
0068
0069 BOOST_ASIO_DECL ~posix_thread();
0070
0071
0072 posix_thread& operator=(posix_thread&& other) noexcept
0073 {
0074 arg_ = other.arg_;
0075 other.arg_ = 0;
0076 return *this;
0077 }
0078
0079
0080 bool joinable() const
0081 {
0082 return !!arg_;
0083 }
0084
0085
0086 BOOST_ASIO_DECL void join();
0087
0088
0089 BOOST_ASIO_DECL static std::size_t hardware_concurrency();
0090
0091 private:
0092 friend void* boost_asio_detail_posix_thread_function(void* arg);
0093
0094 class func_base
0095 {
0096 public:
0097 virtual ~func_base() {}
0098 virtual void run() = 0;
0099 virtual void destroy() = 0;
0100 ::pthread_t thread_;
0101 };
0102
0103 template <typename Function, typename Allocator>
0104 class func
0105 : public func_base
0106 {
0107 public:
0108 func(Function f, const Allocator& a)
0109 : f_(f),
0110 allocator_(a)
0111 {
0112 }
0113
0114 virtual void run()
0115 {
0116 f_();
0117 }
0118
0119 virtual void destroy()
0120 {
0121 deallocate_object(allocator_, this);
0122 }
0123
0124 private:
0125 Function f_;
0126 Allocator allocator_;
0127 };
0128
0129 BOOST_ASIO_DECL func_base* start_thread(func_base* arg);
0130
0131 func_base* arg_;
0132 };
0133
0134 }
0135 }
0136 }
0137
0138 #include <boost/asio/detail/pop_options.hpp>
0139
0140 #if defined(BOOST_ASIO_HEADER_ONLY)
0141 # include <boost/asio/detail/impl/posix_thread.ipp>
0142 #endif
0143
0144 #endif
0145
0146 #endif