|
21 | 21 | // template<class F> int lw_thread_create( lw_thread_t & th, F f ); |
22 | 22 | // void lw_thread_join( lw_thread_t th ); |
23 | 23 |
|
24 | | - |
25 | | -#include <boost/config.hpp> |
26 | | -#include <memory> |
27 | | -#include <cerrno> |
28 | | - |
29 | | -#if defined( BOOST_HAS_PTHREADS ) |
30 | | - |
31 | | -#include <pthread.h> |
| 24 | +#include <thread> |
| 25 | +#include <exception> |
32 | 26 |
|
33 | 27 | namespace boost |
34 | 28 | { |
35 | 29 | namespace detail |
36 | 30 | { |
37 | 31 |
|
38 | | -typedef ::pthread_t lw_thread_t; |
39 | | - |
40 | | -inline int lw_thread_create_( lw_thread_t* thread, const pthread_attr_t* attr, void* (*start_routine)( void* ), void* arg ) |
41 | | -{ |
42 | | - return ::pthread_create( thread, attr, start_routine, arg ); |
43 | | -} |
44 | | - |
45 | | -inline void lw_thread_join( lw_thread_t th ) |
46 | | -{ |
47 | | - ::pthread_join( th, 0 ); |
48 | | -} |
| 32 | +using lw_thread_t = std::thread*; |
49 | 33 |
|
50 | | -} // namespace detail |
51 | | -} // namespace boost |
52 | | - |
53 | | -#else // defined( BOOST_HAS_PTHREADS ) |
54 | | - |
55 | | -#include <windows.h> |
56 | | -#include <process.h> |
57 | | - |
58 | | -namespace boost |
59 | | -{ |
60 | | -namespace detail |
| 34 | +template<class F> int lw_thread_create( lw_thread_t& th, F f ) |
61 | 35 | { |
62 | | - |
63 | | -typedef HANDLE lw_thread_t; |
64 | | - |
65 | | -inline int lw_thread_create_( lw_thread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg ) |
66 | | -{ |
67 | | - HANDLE h = (HANDLE)_beginthreadex( 0, 0, start_routine, arg, 0, 0 ); |
68 | | - |
69 | | - if( h != 0 ) |
| 36 | + try |
70 | 37 | { |
71 | | - *thread = h; |
| 38 | + th = new std::thread( f ); |
72 | 39 | return 0; |
73 | 40 | } |
74 | | - else |
| 41 | + catch( std::exception const& ) |
75 | 42 | { |
76 | | - return EAGAIN; |
| 43 | + return -1; |
77 | 44 | } |
78 | 45 | } |
79 | 46 |
|
80 | | -inline void lw_thread_join( lw_thread_t thread ) |
81 | | -{ |
82 | | - ::WaitForSingleObject( thread, INFINITE ); |
83 | | - ::CloseHandle( thread ); |
84 | | -} |
85 | | - |
86 | | -} // namespace detail |
87 | | -} // namespace boost |
88 | | - |
89 | | -#endif // defined( BOOST_HAS_PTHREADS ) |
90 | | - |
91 | | - |
92 | | -namespace boost |
93 | | -{ |
94 | | -namespace detail |
95 | | -{ |
96 | | - |
97 | | -class lw_abstract_thread |
98 | | -{ |
99 | | -public: |
100 | | - |
101 | | - virtual ~lw_abstract_thread() {} |
102 | | - virtual void run() = 0; |
103 | | -}; |
104 | | - |
105 | | -#if defined( BOOST_HAS_PTHREADS ) |
106 | | - |
107 | | -extern "C" inline void * lw_thread_routine( void * pv ) |
108 | | -{ |
109 | | - std::unique_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) ); |
110 | | - |
111 | | - pt->run(); |
112 | | - |
113 | | - return 0; |
114 | | -} |
115 | | - |
116 | | -#else |
117 | | - |
118 | | -inline unsigned __stdcall lw_thread_routine( void * pv ) |
| 47 | +void lw_thread_join( lw_thread_t th ) |
119 | 48 | { |
120 | | - std::unique_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) ); |
121 | | - |
122 | | - pt->run(); |
123 | | - |
124 | | - return 0; |
125 | | -} |
126 | | - |
127 | | -#endif |
128 | | - |
129 | | -template<class F> class lw_thread_impl: public lw_abstract_thread |
130 | | -{ |
131 | | -public: |
132 | | - |
133 | | - explicit lw_thread_impl( F f ): f_( f ) |
134 | | - { |
135 | | - } |
136 | | - |
137 | | - void run() |
138 | | - { |
139 | | - f_(); |
140 | | - } |
141 | | - |
142 | | -private: |
143 | | - |
144 | | - F f_; |
145 | | -}; |
146 | | - |
147 | | -template<class F> int lw_thread_create( lw_thread_t & th, F f ) |
148 | | -{ |
149 | | - std::unique_ptr<lw_abstract_thread> p( new lw_thread_impl<F>( f ) ); |
150 | | - |
151 | | - int r = lw_thread_create_( &th, 0, lw_thread_routine, p.get() ); |
152 | | - |
153 | | - if( r == 0 ) |
154 | | - { |
155 | | - p.release(); |
156 | | - } |
157 | | - |
158 | | - return r; |
| 49 | + th->join(); |
159 | 50 | } |
160 | 51 |
|
161 | 52 | } // namespace detail |
|
0 commit comments