1 #ifndef BOOST_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
2 #define BOOST_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 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
19 #include <boost/config.hpp>
23 // pthread_create, pthread_join
25 #if defined( BOOST_HAS_PTHREADS )
34 typedef HANDLE pthread_t;
36 int pthread_create( pthread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg )
38 HANDLE h = (HANDLE)_beginthreadex( 0, 0, start_routine, arg, 0, 0 );
51 int pthread_join( pthread_t thread, void ** /*value_ptr*/ )
53 ::WaitForSingleObject( thread, INFINITE );
54 ::CloseHandle( thread );
60 // template<class F> int lw_thread_create( pthread_t & pt, F f );
68 class lw_abstract_thread
72 virtual ~lw_abstract_thread() {}
73 virtual void run() = 0;
76 #if defined( BOOST_HAS_PTHREADS )
78 extern "C" void * lw_thread_routine( void * pv )
80 std::auto_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
89 unsigned __stdcall lw_thread_routine( void * pv )
91 std::auto_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
100 template<class F> class lw_thread_impl: public lw_abstract_thread
104 explicit lw_thread_impl( F f ): f_( f )
118 template<class F> int lw_thread_create( pthread_t & pt, F f )
120 std::auto_ptr<lw_abstract_thread> p( new lw_thread_impl<F>( f ) );
122 int r = pthread_create( &pt, 0, lw_thread_routine, p.get() );
132 } // namespace detail
135 #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED