2013-10-20 12:53:53 +00:00
|
|
|
#ifndef THREAD_H
|
|
|
|
#define THREAD_H
|
|
|
|
|
|
|
|
#include "system_global.h"
|
|
|
|
#include <QThread>
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
namespace paysages {
|
|
|
|
namespace system {
|
2013-10-20 12:53:53 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
typedef void *(*ThreadFunction)(void *data);
|
2013-11-03 12:00:31 +00:00
|
|
|
|
2015-08-12 20:11:26 +00:00
|
|
|
/**
|
|
|
|
* System thread
|
2013-10-20 12:53:53 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
class SYSTEMSHARED_EXPORT Thread : private QThread {
|
|
|
|
public:
|
2015-08-12 20:11:26 +00:00
|
|
|
/**
|
|
|
|
* Create a new thread.
|
2013-10-20 12:53:53 +00:00
|
|
|
*
|
|
|
|
* The thread is not started automatically. A call to method start() needs to be done.
|
2015-08-12 20:11:26 +00:00
|
|
|
*
|
|
|
|
* Either the *function* argument should be provided (with the function to call from the
|
|
|
|
* thread), or the *run* method overridden.
|
2013-10-20 12:53:53 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
Thread(ThreadFunction function = 0);
|
2013-10-20 12:53:53 +00:00
|
|
|
|
2015-08-12 20:11:26 +00:00
|
|
|
/**
|
|
|
|
* Start the thread, with custom data.
|
2013-10-20 12:53:53 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
void start(void *data = 0);
|
2013-10-20 12:53:53 +00:00
|
|
|
|
2015-08-12 20:11:26 +00:00
|
|
|
/**
|
|
|
|
* Wait for the thread to end, and collect its result.
|
|
|
|
*
|
|
|
|
* Returns the value returned by the threaded function.
|
2013-10-20 12:53:53 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
void *join();
|
2013-10-20 12:53:53 +00:00
|
|
|
|
2015-08-12 20:11:26 +00:00
|
|
|
/**
|
|
|
|
* Return true if the thread is currently running.
|
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
inline bool isWorking() const {
|
|
|
|
return not isFinished();
|
|
|
|
}
|
2015-08-12 20:11:26 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
static inline void timeSleepMs(unsigned long ms) {
|
|
|
|
QThread::msleep(ms);
|
|
|
|
}
|
2013-11-03 12:00:31 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
protected:
|
2015-08-12 20:11:26 +00:00
|
|
|
/**
|
|
|
|
* Function to reimplement if no ThreadFunction has been passed to the constructor.
|
2013-12-09 10:59:57 +00:00
|
|
|
*/
|
2013-10-20 12:53:53 +00:00
|
|
|
virtual void run();
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void *data;
|
|
|
|
void *result;
|
2013-12-09 10:59:57 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
2013-12-09 10:59:57 +00:00
|
|
|
ThreadFunction function;
|
2013-10-20 12:53:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // THREAD_H
|