3#if defined(_WIN32) && defined(_MSC_VER)
24Thread::Thread(
const string& name,
size_t stackSize): name(name), pThreadCreated(false), stopRequested(false), stackSize(stackSize) {
31 return std::thread::hardware_concurrency();
35 #if defined(CPPTHREADS)
42 #if defined(_WIN32) && defined(_MSC_VER)
45 uint64_t secondsWaited = 0L;
46 while (milliseconds - (secondsWaited * 1000L) >= 1000L) {
50 usleep((milliseconds - (secondsWaited * 1000L)) * 1000L);
56 #if defined(CPPTHREADS)
60 auto result = pthread_join(
pThread, &status);
66 #if defined(CPPTHREADS)
67 thread =
new std::thread(
pThreadRun, (
void*)
this);
70 pthread_attr_t pThreadAttr;
71 pthread_attr_init(&pThreadAttr);
73 pthread_attr_setdetachstate(&pThreadAttr, PTHREAD_CREATE_JOINABLE);
75 size_t currentStackSize;
76 pthread_attr_getstacksize(&pThreadAttr, ¤tStackSize);
78 Console::println(
"Thread::start(): " +
name +
": changing stack size from " + to_string(currentStackSize) +
" to " + to_string(
stackSize));
79 pthread_attr_setstacksize(&pThreadAttr,
stackSize);
#define PTHREAD_CHECK_ERROR(NAME, MSG, FUNCTION)
virtual void run()=0
Abstract run() method, should be implemented by subclassed class, will be called after spawn by start...
static void * pThreadRun(void *thread)
static int getHardwareThreadCount()
void start()
Starts this objects thread.
void join()
Blocks caller thread until this thread has been terminated.
volatile bool stopRequested
virtual ~Thread()
Public destructor.
void stop()
Requests that this thread should be stopped.
static void sleep(const uint64_t milliseconds)
sleeps current thread for given time in milliseconds
bool isStopRequested()
Returns if stop has been requested.