diff --git a/extras/test/shared_ptr_mt_test.cpp b/extras/test/shared_ptr_mt_test.cpp index 50a9f51..8a50425 100644 --- a/extras/test/shared_ptr_mt_test.cpp +++ b/extras/test/shared_ptr_mt_test.cpp @@ -62,7 +62,7 @@ int main() clock_t t = clock(); - pthread_t a[ m ]; + boost::detail::lw_thread_t a[ m ]; for( int i = 0; i < m; ++i ) { @@ -71,7 +71,7 @@ int main() for( int j = 0; j < m; ++j ) { - pthread_join( a[j], 0 ); + boost::detail::lw_thread_join( a[j] ); } t = clock() - t; diff --git a/extras/test/sp_atomic_mt2_test.cpp b/extras/test/sp_atomic_mt2_test.cpp index 6a2e44e..750b0b2 100644 --- a/extras/test/sp_atomic_mt2_test.cpp +++ b/extras/test/sp_atomic_mt2_test.cpp @@ -228,7 +228,7 @@ int main( int ac, char const * av[] ) clock_t t = clock(); - std::vector a( m ); + std::vector a( m ); for( int i = 0; i < m; ++i ) { @@ -237,7 +237,7 @@ int main( int ac, char const * av[] ) for( int j = 0; j < m; ++j ) { - pthread_join( a[ j ], 0 ); + boost::detail::lw_thread_join( a[ j ] ); } t = clock() - t; diff --git a/extras/test/sp_atomic_mt_test.cpp b/extras/test/sp_atomic_mt_test.cpp index 0f0bf68..32c76c5 100644 --- a/extras/test/sp_atomic_mt_test.cpp +++ b/extras/test/sp_atomic_mt_test.cpp @@ -165,7 +165,7 @@ int main() clock_t t = clock(); - pthread_t a[ mr+mw ]; + boost::detail::lw_thread_t a[ mr+mw ]; for( int i = 0; i < mr; ++i ) { @@ -179,7 +179,7 @@ int main() for( int j = 0; j < mr+mw; ++j ) { - pthread_join( a[ j ], 0 ); + boost::detail::lw_thread_join( a[ j ] ); } t = clock() - t; diff --git a/extras/test/weak_ptr_mt_test.cpp b/extras/test/weak_ptr_mt_test.cpp index 04439e6..5b11ff9 100644 --- a/extras/test/weak_ptr_mt_test.cpp +++ b/extras/test/weak_ptr_mt_test.cpp @@ -100,7 +100,7 @@ int main() clock_t t = clock(); - pthread_t a[ m ]; + boost::detail::lw_thread_t a[ m ]; for( int i = 0; i < m; ++i ) { @@ -111,7 +111,7 @@ int main() for( int j = 0; j < m; ++j ) { - pthread_join( a[j], 0 ); + boost::detail::lw_thread_join( a[j] ); } t = clock() - t; diff --git a/include/boost/detail/lightweight_thread.hpp b/include/boost/detail/lightweight_thread.hpp index 2145815..c2930d5 100644 --- a/include/boost/detail/lightweight_thread.hpp +++ b/include/boost/detail/lightweight_thread.hpp @@ -10,30 +10,59 @@ // boost/detail/lightweight_thread.hpp // // Copyright (c) 2002 Peter Dimov and Multi Media Ltd. -// Copyright (c) 2008 Peter Dimov +// Copyright (c) 2008, 2018 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt +// +// +// typedef /*...*/ lw_thread_t; // as pthread_t +// template int lw_thread_create( lw_thread_t & th, F f ); +// void lw_thread_join( lw_thread_t th ); + #include #include #include -// pthread_create, pthread_join - #if defined( BOOST_HAS_PTHREADS ) #include -#else +namespace boost +{ +namespace detail +{ + +typedef ::pthread_t lw_thread_t; + +inline int lw_thread_create_( lw_thread_t* thread, const pthread_attr_t* attr, void* (*start_routine)( void* ), void* arg ) +{ + return ::pthread_create( thread, attr, start_routine, arg ); +} + +inline void lw_thread_join( lw_thread_t th ) +{ + ::pthread_join( th, 0 ); +} + +} // namespace detail +} // namespace boost + +#else // defined( BOOST_HAS_PTHREADS ) #include #include -typedef HANDLE pthread_t; +namespace boost +{ +namespace detail +{ -int pthread_create( pthread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg ) +typedef HANDLE lw_thread_t; + +inline int lw_thread_create_( lw_thread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg ) { HANDLE h = (HANDLE)_beginthreadex( 0, 0, start_routine, arg, 0, 0 ); @@ -48,20 +77,20 @@ int pthread_create( pthread_t * thread, void const *, unsigned (__stdcall * star } } -int pthread_join( pthread_t thread, void ** /*value_ptr*/ ) +inline void lw_thread_join( lw_thread_t thread ) { ::WaitForSingleObject( thread, INFINITE ); ::CloseHandle( thread ); - return 0; } -#endif +} // namespace detail +} // namespace boost + +#endif // defined( BOOST_HAS_PTHREADS ) -// template int lw_thread_create( pthread_t & pt, F f ); namespace boost { - namespace detail { @@ -131,7 +160,7 @@ private: F f_; }; -template int lw_thread_create( pthread_t & pt, F f ) +template int lw_thread_create( lw_thread_t & th, F f ) { #if defined(BOOST_NO_CXX11_SMART_PTR) @@ -143,7 +172,7 @@ template int lw_thread_create( pthread_t & pt, F f ) #endif - int r = pthread_create( &pt, 0, lw_thread_routine, p.get() ); + int r = lw_thread_create_( &th, 0, lw_thread_routine, p.get() ); if( r == 0 ) { diff --git a/test/lw_thread_test.cpp b/test/lw_thread_test.cpp index 1da8ed5..1fe2274 100644 --- a/test/lw_thread_test.cpp +++ b/test/lw_thread_test.cpp @@ -18,7 +18,7 @@ void f() int main() { int const N = 4; - pthread_t th[ N ] = {}; + boost::detail::lw_thread_t th[ N ] = {}; for( int i = 0; i < N; ++i ) { @@ -27,7 +27,7 @@ int main() for( int i = 0; i < N; ++i ) { - pthread_join( th[ i ], 0 ); + boost::detail::lw_thread_join( th[ i ] ); } BOOST_TEST_EQ( count, N );