Skip to content

Commit f2b590e

Browse files
committed
Implement lightweight_thread.hpp in terms of <thread>
1 parent 341fdb4 commit f2b590e

File tree

1 file changed

+10
-119
lines changed

1 file changed

+10
-119
lines changed

include/boost/smart_ptr/detail/lightweight_thread.hpp

Lines changed: 10 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -21,141 +21,32 @@
2121
// template<class F> int lw_thread_create( lw_thread_t & th, F f );
2222
// void lw_thread_join( lw_thread_t th );
2323

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>
3226

3327
namespace boost
3428
{
3529
namespace detail
3630
{
3731

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*;
4933

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 )
6135
{
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
7037
{
71-
*thread = h;
38+
th = new std::thread( f );
7239
return 0;
7340
}
74-
else
41+
catch( std::exception const& )
7542
{
76-
return EAGAIN;
43+
return -1;
7744
}
7845
}
7946

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 )
11948
{
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();
15950
}
16051

16152
} // namespace detail

0 commit comments

Comments
 (0)