1 #ifndef BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // boost/detail/lightweight_thread.hpp
12 // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
13 // Copyright (c) 2008, 2018 Peter Dimov
15 // Distributed under the Boost Software License, Version 1.0.
16 // See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt
20 // typedef /*...*/ lw_thread_t; // as pthread_t
21 // template<class F> int lw_thread_create( lw_thread_t & th, F f );
22 // void lw_thread_join( lw_thread_t th );
25 #include <boost/config.hpp>
29 #if defined( BOOST_HAS_PTHREADS )
38 typedef ::pthread_t lw_thread_t;
40 inline int lw_thread_create_( lw_thread_t* thread, const pthread_attr_t* attr, void* (*start_routine)( void* ), void* arg )
42 return ::pthread_create( thread, attr, start_routine, arg );
45 inline void lw_thread_join( lw_thread_t th )
47 ::pthread_join( th, 0 );
53 #else // defined( BOOST_HAS_PTHREADS )
63 typedef HANDLE lw_thread_t;
65 inline int lw_thread_create_( lw_thread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg )
67 HANDLE h = (HANDLE)_beginthreadex( 0, 0, start_routine, arg, 0, 0 );
80 inline void lw_thread_join( lw_thread_t thread )
82 ::WaitForSingleObject( thread, INFINITE );
83 ::CloseHandle( thread );
89 #endif // defined( BOOST_HAS_PTHREADS )
97 class lw_abstract_thread
101 virtual ~lw_abstract_thread() {}
102 virtual void run() = 0;
105 #if defined( BOOST_HAS_PTHREADS )
107 extern "C" void * lw_thread_routine( void * pv )
109 #if defined(BOOST_NO_CXX11_SMART_PTR)
111 std::auto_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
115 std::unique_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
126 unsigned __stdcall lw_thread_routine( void * pv )
128 #if defined(BOOST_NO_CXX11_SMART_PTR)
130 std::auto_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
134 std::unique_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
145 template<class F> class lw_thread_impl: public lw_abstract_thread
149 explicit lw_thread_impl( F f ): f_( f )
163 template<class F> int lw_thread_create( lw_thread_t & th, F f )
165 #if defined(BOOST_NO_CXX11_SMART_PTR)
167 std::auto_ptr<lw_abstract_thread> p( new lw_thread_impl<F>( f ) );
171 std::unique_ptr<lw_abstract_thread> p( new lw_thread_impl<F>( f ) );
175 int r = lw_thread_create_( &th, 0, lw_thread_routine, p.get() );
185 } // namespace detail
188 #endif // #ifndef BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED