Do not define pthread_* in lightweight_thread.hpp; mingw-w64 includes pthread.h in the standard library headers

This commit is contained in:
Peter Dimov
2018-03-08 01:19:44 +02:00
parent 5885763287
commit d1600b8abc
6 changed files with 52 additions and 23 deletions

View File

@ -62,7 +62,7 @@ int main()
clock_t t = clock(); clock_t t = clock();
pthread_t a[ m ]; boost::detail::lw_thread_t a[ m ];
for( int i = 0; i < m; ++i ) for( int i = 0; i < m; ++i )
{ {
@ -71,7 +71,7 @@ int main()
for( int j = 0; j < m; ++j ) for( int j = 0; j < m; ++j )
{ {
pthread_join( a[j], 0 ); boost::detail::lw_thread_join( a[j] );
} }
t = clock() - t; t = clock() - t;

View File

@ -228,7 +228,7 @@ int main( int ac, char const * av[] )
clock_t t = clock(); clock_t t = clock();
std::vector<pthread_t> a( m ); std::vector<boost::detail::lw_thread_t> a( m );
for( int i = 0; i < m; ++i ) 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 ) for( int j = 0; j < m; ++j )
{ {
pthread_join( a[ j ], 0 ); boost::detail::lw_thread_join( a[ j ] );
} }
t = clock() - t; t = clock() - t;

View File

@ -165,7 +165,7 @@ int main()
clock_t t = clock(); clock_t t = clock();
pthread_t a[ mr+mw ]; boost::detail::lw_thread_t a[ mr+mw ];
for( int i = 0; i < mr; ++i ) for( int i = 0; i < mr; ++i )
{ {
@ -179,7 +179,7 @@ int main()
for( int j = 0; j < mr+mw; ++j ) for( int j = 0; j < mr+mw; ++j )
{ {
pthread_join( a[ j ], 0 ); boost::detail::lw_thread_join( a[ j ] );
} }
t = clock() - t; t = clock() - t;

View File

@ -100,7 +100,7 @@ int main()
clock_t t = clock(); clock_t t = clock();
pthread_t a[ m ]; boost::detail::lw_thread_t a[ m ];
for( int i = 0; i < m; ++i ) for( int i = 0; i < m; ++i )
{ {
@ -111,7 +111,7 @@ int main()
for( int j = 0; j < m; ++j ) for( int j = 0; j < m; ++j )
{ {
pthread_join( a[j], 0 ); boost::detail::lw_thread_join( a[j] );
} }
t = clock() - t; t = clock() - t;

View File

@ -10,30 +10,59 @@
// boost/detail/lightweight_thread.hpp // boost/detail/lightweight_thread.hpp
// //
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd. // 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. // Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at // See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt // http://www.boost.org/LICENSE_1_0.txt
//
//
// typedef /*...*/ lw_thread_t; // as pthread_t
// template<class F> int lw_thread_create( lw_thread_t & th, F f );
// void lw_thread_join( lw_thread_t th );
#include <boost/config.hpp> #include <boost/config.hpp>
#include <memory> #include <memory>
#include <cerrno> #include <cerrno>
// pthread_create, pthread_join
#if defined( BOOST_HAS_PTHREADS ) #if defined( BOOST_HAS_PTHREADS )
#include <pthread.h> #include <pthread.h>
#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 <windows.h> #include <windows.h>
#include <process.h> #include <process.h>
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 ); 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 ); ::WaitForSingleObject( thread, INFINITE );
::CloseHandle( thread ); ::CloseHandle( thread );
return 0;
} }
#endif } // namespace detail
} // namespace boost
#endif // defined( BOOST_HAS_PTHREADS )
// template<class F> int lw_thread_create( pthread_t & pt, F f );
namespace boost namespace boost
{ {
namespace detail namespace detail
{ {
@ -131,7 +160,7 @@ private:
F f_; F f_;
}; };
template<class F> int lw_thread_create( pthread_t & pt, F f ) template<class F> int lw_thread_create( lw_thread_t & th, F f )
{ {
#if defined(BOOST_NO_CXX11_SMART_PTR) #if defined(BOOST_NO_CXX11_SMART_PTR)
@ -143,7 +172,7 @@ template<class F> int lw_thread_create( pthread_t & pt, F f )
#endif #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 ) if( r == 0 )
{ {

View File

@ -18,7 +18,7 @@ void f()
int main() int main()
{ {
int const N = 4; int const N = 4;
pthread_t th[ N ] = {}; boost::detail::lw_thread_t th[ N ] = {};
for( int i = 0; i < N; ++i ) for( int i = 0; i < N; ++i )
{ {
@ -27,7 +27,7 @@ int main()
for( int i = 0; i < N; ++i ) for( int i = 0; i < N; ++i )
{ {
pthread_join( th[ i ], 0 ); boost::detail::lw_thread_join( th[ i ] );
} }
BOOST_TEST_EQ( count, N ); BOOST_TEST_EQ( count, N );