72 lines
1.4 KiB
C++
72 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include "core_global.h"
|
|
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <chrono>
|
|
|
|
namespace paysages {
|
|
namespace system {
|
|
|
|
typedef void *(*ThreadFunction)(void *data);
|
|
|
|
/**
|
|
* System thread
|
|
*/
|
|
class CORESHARED_EXPORT Thread {
|
|
public:
|
|
/**
|
|
* Create a new thread.
|
|
*
|
|
* The thread is not started automatically. A call to method start() needs to be done.
|
|
*
|
|
* Either the 'function' argument should be provided (with the function to call from the
|
|
* thread), or the 'run' method overridden.
|
|
*/
|
|
Thread(ThreadFunction function = 0);
|
|
|
|
virtual ~Thread();
|
|
|
|
/**
|
|
* Start the thread, with custom data.
|
|
*/
|
|
void start(void *data = 0);
|
|
|
|
/**
|
|
* Wait for the thread to end, and collect its result.
|
|
*
|
|
* Returns the value returned by the threaded function.
|
|
*/
|
|
void *join();
|
|
|
|
/**
|
|
* Return true if the thread is currently running.
|
|
*/
|
|
inline bool isWorking() const {
|
|
return systhread->joinable();
|
|
}
|
|
|
|
static inline void timeSleepMs(unsigned long ms) {
|
|
this_thread::sleep_for(chrono::milliseconds(ms));
|
|
}
|
|
|
|
protected:
|
|
/**
|
|
* Function to reimplement if no ThreadFunction has been passed to the constructor.
|
|
*/
|
|
virtual void run();
|
|
|
|
void *data;
|
|
void *result;
|
|
|
|
private:
|
|
void wait();
|
|
|
|
ThreadFunction function;
|
|
thread* systhread;
|
|
mutex go;
|
|
};
|
|
}
|
|
}
|