lightweight_thread.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
00002 #define BOOST_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //  boost/detail/lightweight_thread.hpp
00011 //
00012 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
00013 //  Copyright (c) 2008 Peter Dimov
00014 //
00015 //  Distributed under the Boost Software License, Version 1.0.
00016 //  See accompanying file LICENSE_1_0.txt or copy at
00017 //  http://www.boost.org/LICENSE_1_0.txt
00018 
00019 #include <boost/config.hpp>
00020 #include <memory>
00021 #include <cerrno>
00022 
00023 // pthread_create, pthread_join
00024 
00025 #if defined( BOOST_HAS_PTHREADS )
00026 
00027 #include <pthread.h>
00028 
00029 #else
00030 
00031 #include <windows.h>
00032 #include <process.h>
00033 
00034 typedef HANDLE pthread_t;
00035 
00036 int pthread_create( pthread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg )
00037 {
00038     HANDLE h = (HANDLE)_beginthreadex( 0, 0, start_routine, arg, 0, 0 );
00039 
00040     if( h != 0 )
00041     {
00042         *thread = h;
00043         return 0;
00044     }
00045     else
00046     {
00047         return EAGAIN;
00048     }
00049 }
00050 
00051 int pthread_join( pthread_t thread, void ** /*value_ptr*/ )
00052 {
00053     ::WaitForSingleObject( thread, INFINITE );
00054     ::CloseHandle( thread );
00055     return 0;
00056 }
00057 
00058 #endif
00059 
00060 // template<class F> int lw_thread_create( pthread_t & pt, F f );
00061 
00062 namespace boost
00063 {
00064 
00065 namespace detail
00066 {
00067 
00068 class lw_abstract_thread
00069 {
00070 public:
00071 
00072     virtual ~lw_abstract_thread() {}
00073     virtual void run() = 0;
00074 };
00075 
00076 #if defined( BOOST_HAS_PTHREADS )
00077 
00078 extern "C" void * lw_thread_routine( void * pv )
00079 {
00080     std::auto_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
00081 
00082     pt->run();
00083 
00084     return 0;
00085 }
00086 
00087 #else
00088 
00089 unsigned __stdcall lw_thread_routine( void * pv )
00090 {
00091     std::auto_ptr<lw_abstract_thread> pt( static_cast<lw_abstract_thread *>( pv ) );
00092 
00093     pt->run();
00094 
00095     return 0;
00096 }
00097 
00098 #endif
00099 
00100 template<class F> class lw_thread_impl: public lw_abstract_thread
00101 {
00102 public:
00103 
00104     explicit lw_thread_impl( F f ): f_( f )
00105     {
00106     }
00107 
00108     void run()
00109     {
00110         f_();
00111     }
00112 
00113 private:
00114 
00115     F f_;
00116 };
00117 
00118 template<class F> int lw_thread_create( pthread_t & pt, F f )
00119 {
00120     std::auto_ptr<lw_abstract_thread> p( new lw_thread_impl<F>( f ) );
00121 
00122     int r = pthread_create( &pt, 0, lw_thread_routine, p.get() );
00123 
00124     if( r == 0 )
00125     {
00126         p.release();
00127     }
00128 
00129     return r;
00130 }
00131 
00132 } // namespace detail
00133 } // namespace boost
00134 
00135 #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29