#ifndef GLTB_THREAD_H #define GLTB_THREAD_H #ifdef WIN32 #include #else #include #include #endif #include "GLTB/referencedobject.h" #include "GLTB/rwlock.h" namespace gltb { /** * Interface for classes that want to to be run as threads. Thread instances * call the run method when starting the new thread. */ class Runnable { public: virtual ~Runnable() {}; virtual void run() = 0; }; /** * Generic platform-independent interface for threads. */ class Thread : public ReferencedObject { private: Thread(Runnable *runnable); public: ~Thread(); void start(); void run(); void join(); void kill(); //< kill this thread hard void assignToProcessor(int processor); unsigned int getUniqueID(); static Thread *createThread(Runnable *runnable); static unsigned int getCurrentThreadUniqueID(); static unsigned int getCurrentProcessor(); protected: Runnable *runnable; #ifdef WIN32 HANDLE threadHandle; unsigned int threadID; static DWORD WINAPI runner(LPVOID thread); #else pthread_t thread; unsigned int threadID; static void *runner(void *thread); static unsigned int threadIDCounter; static std::map threadIDMap; static gltb::RefPtr threadIDMapLock; #endif }; } #endif