Clementine-audio-player-Mac.../src/backgroundthread.h

60 lines
1.0 KiB
C
Raw Normal View History

2009-12-24 20:16:07 +01:00
#ifndef BACKGROUNDTHREAD_H
#define BACKGROUNDTHREAD_H
#include <QThread>
#include <QtDebug>
#include <boost/shared_ptr.hpp>
class BackgroundThreadBase : public QThread {
Q_OBJECT
public:
BackgroundThreadBase(QObject* parent = 0) : QThread(parent) {}
signals:
void Initialised();
};
template <typename T>
class BackgroundThread : public BackgroundThreadBase {
public:
BackgroundThread(QObject* parent = 0);
~BackgroundThread();
boost::shared_ptr<T> Worker() const { return worker_; }
protected:
void run();
private:
boost::shared_ptr<T> worker_;
};
template <typename T>
BackgroundThread<T>::BackgroundThread(QObject *parent)
: BackgroundThreadBase(parent)
{
}
template <typename T>
BackgroundThread<T>::~BackgroundThread() {
if (isRunning()) {
quit();
if (wait(10000))
return;
terminate();
wait(10000);
}
}
template <typename T>
void BackgroundThread<T>::run() {
worker_.reset(new T);
emit Initialised();
exec();
worker_.reset();
}
#endif // BACKGROUNDTHREAD_H