diff options
Diffstat (limited to 'dep/efsw/src/efsw/Thread.hpp')
| -rw-r--r-- | dep/efsw/src/efsw/Thread.hpp | 107 |
1 files changed, 28 insertions, 79 deletions
diff --git a/dep/efsw/src/efsw/Thread.hpp b/dep/efsw/src/efsw/Thread.hpp index b60373c2075..5a5d470e10b 100644 --- a/dep/efsw/src/efsw/Thread.hpp +++ b/dep/efsw/src/efsw/Thread.hpp @@ -2,99 +2,48 @@ #define EFSW_THREAD_HPP #include <efsw/base.hpp> +#include <functional> +#include <memory> +#include <thread> namespace efsw { -namespace Platform { -class ThreadImpl; -} -namespace Private { -struct ThreadFunc; -} - /** @brief Thread manager class */ class Thread { public: - typedef void ( *FuncType )( void* ); - - template <typename F> Thread( F function ); - - template <typename F, typename A> Thread( F function, A argument ); - template <typename C> Thread( void ( C::*function )(), C* object ); + Thread(std::function<void()> fun) + : mFun{std::move(fun)} + { + } - virtual ~Thread(); + ~Thread() + { + wait(); + } /** Launch the thread */ - virtual void launch(); + void launch() + { + if (!mThread) + mThread.reset(new std::thread{std::move(mFun)}); + } /** Wait the thread until end */ - void wait(); - - /** Terminate the thread */ - void terminate(); - - protected: - Thread(); - - private: - friend class Platform::ThreadImpl; - - /** The virtual function to run in the thread */ - virtual void run(); - - Platform::ThreadImpl* mThreadImpl; ///< OS-specific implementation of the thread - Private::ThreadFunc* mEntryPoint; ///< Abstraction of the function to run -}; - -//! NOTE: Taken from SFML2 threads -namespace Private { - -// Base class for abstract thread functions -struct ThreadFunc { - virtual ~ThreadFunc() {} - virtual void run() = 0; + void wait() + { + if (mThread) + { + mThread->join(); + mThread.reset(); + } + } +private: + + std::unique_ptr<std::thread> mThread; + std::function<void()> mFun; }; -// Specialization using a functor (including free functions) with no argument -template <typename T> struct ThreadFunctor : ThreadFunc { - ThreadFunctor( T functor ) : m_functor( functor ) {} - virtual void run() { m_functor(); } - T m_functor; -}; - -// Specialization using a functor (including free functions) with one argument -template <typename F, typename A> struct ThreadFunctorWithArg : ThreadFunc { - ThreadFunctorWithArg( F function, A arg ) : m_function( function ), m_arg( arg ) {} - virtual void run() { m_function( m_arg ); } - F m_function; - A m_arg; -}; - -// Specialization using a member function -template <typename C> struct ThreadMemberFunc : ThreadFunc { - ThreadMemberFunc( void ( C::*function )(), C* object ) : - m_function( function ), m_object( object ) {} - virtual void run() { ( m_object->*m_function )(); } - void ( C::*m_function )(); - C* m_object; -}; - -} // namespace Private - -template <typename F> -Thread::Thread( F functor ) : - mThreadImpl( NULL ), mEntryPoint( new Private::ThreadFunctor<F>( functor ) ) {} - -template <typename F, typename A> -Thread::Thread( F function, A argument ) : - mThreadImpl( NULL ), - mEntryPoint( new Private::ThreadFunctorWithArg<F efCOMMA A>( function, argument ) ) {} - -template <typename C> -Thread::Thread( void ( C::*function )(), C* object ) : - mThreadImpl( NULL ), mEntryPoint( new Private::ThreadMemberFunc<C>( function, object ) ) {} - } // namespace efsw #endif |
