TracyThread.hpp (1290B)
1 #ifndef __TRACYTHREAD_HPP__ 2 #define __TRACYTHREAD_HPP__ 3 4 #if defined _WIN32 || defined __CYGWIN__ 5 # include <windows.h> 6 #else 7 # include <pthread.h> 8 #endif 9 10 namespace tracy 11 { 12 13 #if defined _WIN32 || defined __CYGWIN__ 14 15 class Thread 16 { 17 public: 18 Thread( void(*func)( void* ptr ), void* ptr ) 19 : m_func( func ) 20 , m_ptr( ptr ) 21 , m_hnd( CreateThread( nullptr, 0, Launch, this, 0, nullptr ) ) 22 {} 23 24 ~Thread() 25 { 26 WaitForSingleObject( m_hnd, INFINITE ); 27 CloseHandle( m_hnd ); 28 } 29 30 HANDLE Handle() const { return m_hnd; } 31 32 private: 33 static DWORD WINAPI Launch( void* ptr ) { ((Thread*)ptr)->m_func( ((Thread*)ptr)->m_ptr ); return 0; } 34 35 void(*m_func)( void* ptr ); 36 void* m_ptr; 37 HANDLE m_hnd; 38 }; 39 40 #else 41 42 class Thread 43 { 44 public: 45 Thread( void(*func)( void* ptr ), void* ptr ) 46 : m_func( func ) 47 , m_ptr( ptr ) 48 { 49 pthread_create( &m_thread, nullptr, Launch, this ); 50 } 51 52 ~Thread() 53 { 54 pthread_join( m_thread, nullptr ); 55 } 56 57 pthread_t Handle() const { return m_thread; } 58 59 private: 60 static void* Launch( void* ptr ) { ((Thread*)ptr)->m_func( ((Thread*)ptr)->m_ptr ); return nullptr; } 61 void(*m_func)( void* ptr ); 62 void* m_ptr; 63 pthread_t m_thread; 64 }; 65 66 #endif 67 68 } 69 70 #endif