Commit 85584487 authored by Thiago C. Santini's avatar Thiago C. Santini

Adds thread manager

We now use the thread manager object to signal the other
objects to finish. The sending of finished by the thread should not reach the
thread's object (as the event loop is already finished by the time it is
emmited) although it did during initial testing.
parent c685aba4
...@@ -19,7 +19,8 @@ SOURCES += \ ...@@ -19,7 +19,8 @@ SOURCES += \
$${TOP}/src/PerfCounter.cpp \ $${TOP}/src/PerfCounter.cpp \
$${TOP}/src/Calibration.cpp \ $${TOP}/src/Calibration.cpp \
$${TOP}/src/settings.cpp \ $${TOP}/src/settings.cpp \
$${TOP}/src/utils.cpp $${TOP}/src/utils.cpp \
src/ThreadManager.cpp
HEADERS += \ HEADERS += \
$${TOP}/src/ImageProcessing.h \ $${TOP}/src/ImageProcessing.h \
...@@ -50,7 +51,8 @@ HEADERS += \ ...@@ -50,7 +51,8 @@ HEADERS += \
$${TOP}/else/blob_gen.h \ $${TOP}/else/blob_gen.h \
$${TOP}/else/canny_impl.h \ $${TOP}/else/canny_impl.h \
$${TOP}/else/filter_edges.h \ $${TOP}/else/filter_edges.h \
$${TOP}/else/find_best_edge.h $${TOP}/else/find_best_edge.h \
src/ThreadManager.h
FORMS += \ FORMS += \
$${TOP}/src/Gui.ui $${TOP}/src/Gui.ui
......
...@@ -60,10 +60,11 @@ void ImageAcquisition::onThreadStarted(){ ...@@ -60,10 +60,11 @@ void ImageAcquisition::onThreadStarted(){
slaveThread = QtConcurrent::run(updateSlave, this); slaveThread = QtConcurrent::run(updateSlave, this);
} }
void ImageAcquisition::onThreadFinished() void ImageAcquisition::onThreadFinishing()
{ {
running = false; running = false;
slaveThread.waitForFinished(); slaveThread.waitForFinished();
qDebug() << QThread::currentThread()->objectName() << " done.";
} }
ImageAcquisition::ImageAcquisition(QObject *parent) : QObject(parent), ImageAcquisition::ImageAcquisition(QObject *parent) : QObject(parent),
......
...@@ -62,7 +62,7 @@ signals: ...@@ -62,7 +62,7 @@ signals:
public slots: public slots:
void onThreadStarted(); void onThreadStarted();
void onThreadFinished(); void onThreadFinishing();
}; };
......
...@@ -14,8 +14,9 @@ void ImageProcessing::onThreadStarted(){ ...@@ -14,8 +14,9 @@ void ImageProcessing::onThreadStarted(){
m_running = true; m_running = true;
} }
void ImageProcessing::onThreadFinished() void ImageProcessing::onThreadFinishing()
{ {
qDebug() << QThread::currentThread()->objectName() << " done.";
} }
void ImageProcessing::project(JournalEntry &journalEntry) void ImageProcessing::project(JournalEntry &journalEntry)
......
#ifndef IMAGEPROCESSING_H #ifndef IMAGEPROCESSING_H
#define IMAGEPROCESSING_H #define IMAGEPROCESSING_H
#include <qobject.h> #include <QObject>
#include <QTime> #include <QTime>
#include <QPoint> #include <QPoint>
#include <qdebug.h> #include <QThread>
#include <QDebug>
#include <opencv/cv.h> #include <opencv/cv.h>
#include "Journal.h" #include "Journal.h"
...@@ -36,7 +38,7 @@ public: ...@@ -36,7 +38,7 @@ public:
public slots: public slots:
void onThreadStarted(); void onThreadStarted();
void onThreadFinished(); void onThreadFinishing();
void processImage(JournalEntry journalEntry); void processImage(JournalEntry journalEntry);
signals: signals:
......
...@@ -130,8 +130,9 @@ void Journal::onThreadStarted() ...@@ -130,8 +130,9 @@ void Journal::onThreadStarted()
{ {
} }
void Journal::onThreadFinished() void Journal::onThreadFinishing()
{ {
qDebug() << QThread::currentThread()->objectName() << " done.";
} }
void Journal::setOutput() void Journal::setOutput()
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <QDir> #include <QDir>
#include <QFileInfoList> #include <QFileInfoList>
#include <QRegExp> #include <QRegExp>
#include <QThread>
#include "opencv2/core/core.hpp" #include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp" #include "opencv2/highgui/highgui.hpp"
...@@ -97,7 +98,7 @@ public slots: ...@@ -97,7 +98,7 @@ public slots:
void store(JournalEntry journalEntry); void store(JournalEntry journalEntry);
void setRecording(bool val); void setRecording(bool val);
void onThreadStarted(); void onThreadStarted();
void onThreadFinished(); void onThreadFinishing();
signals: signals:
void dumpCalibration(QString idxStr); void dumpCalibration(QString idxStr);
......
...@@ -22,7 +22,7 @@ void NetworkStream::onThreadStarted() ...@@ -22,7 +22,7 @@ void NetworkStream::onThreadStarted()
} }
} }
void NetworkStream::onThreadFinished() void NetworkStream::onThreadFinishing()
{ {
if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) { if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
tcpServer->close(); tcpServer->close();
...@@ -32,6 +32,7 @@ void NetworkStream::onThreadFinished() ...@@ -32,6 +32,7 @@ void NetworkStream::onThreadFinished()
} else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) { } else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) {
delete socket; socket; delete socket; socket;
} }
qDebug() << QThread::currentThread()->objectName() << " done.";
} }
void NetworkStream::openSocket() void NetworkStream::openSocket()
......
...@@ -21,7 +21,7 @@ public slots: ...@@ -21,7 +21,7 @@ public slots:
void closeSocket(); void closeSocket();
void push(JournalEntry journalEntry); void push(JournalEntry journalEntry);
void onThreadStarted(); void onThreadStarted();
void onThreadFinished(); void onThreadFinishing();
void timerEvent(QTimerEvent *event); void timerEvent(QTimerEvent *event);
private: private:
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "ImageAcquisition.h" #include "ImageAcquisition.h"
#include "ImageProcessing.h" #include "ImageProcessing.h"
#include "NetworkStream.h" #include "NetworkStream.h"
#include "ThreadManager.h"
#include "Journal.h" #include "Journal.h"
#include "Gui.h" #include "Gui.h"
...@@ -14,22 +15,6 @@ ...@@ -14,22 +15,6 @@
#define VERSION "2.0.0" #define VERSION "2.0.0"
static QThread* runInOwnThread(QObject &object, QString name, QThread::Priority priority)
{
QThread *thread = new QThread();
thread->setObjectName(name);
object.moveToThread(thread);
QThread::connect(thread, SIGNAL(started()), &object, SLOT(onThreadStarted()));
QThread::connect(thread, SIGNAL(finished()), &object, SLOT(onThreadFinished()));
QThread::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start(priority);
return thread;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
...@@ -115,12 +100,13 @@ int main(int argc, char *argv[]) ...@@ -115,12 +100,13 @@ int main(int argc, char *argv[])
QThread::currentThread()->setObjectName("Gui"); QThread::currentThread()->setObjectName("Gui");
// Set priorities // Thread Management
ThreadManager threadManager;
QThread::currentThread()->setPriority(QThread::NormalPriority); QThread::currentThread()->setPriority(QThread::NormalPriority);
QThread *imageAcquisitionThread = runInOwnThread(imageAcquisition, "ImageAcquisitionThread", QThread::TimeCriticalPriority); threadManager.newThread(imageAcquisition, "ImageAcquisitionThread", QThread::TimeCriticalPriority);
QThread *imageProcessingThread = runInOwnThread(imageProcessing, "ImageProcessingThread", QThread::TimeCriticalPriority); threadManager.newThread(imageProcessing, "ImageProcessingThread", QThread::TimeCriticalPriority);
QThread *NetworkStreamThread = runInOwnThread(networkStream, "NetworkStreamThread", QThread::TimeCriticalPriority); threadManager.newThread(networkStream, "NetworkStreamThread", QThread::TimeCriticalPriority);
QThread *journalThread = runInOwnThread(journal, "JournalThread", QThread::NormalPriority); threadManager.newThread(journal, "JournalThread", QThread::NormalPriority);
// Ready to start // Ready to start
QRect screenGeometry = QApplication::desktop()->screenGeometry(); QRect screenGeometry = QApplication::desktop()->screenGeometry();
...@@ -134,17 +120,7 @@ int main(int argc, char *argv[]) ...@@ -134,17 +120,7 @@ int main(int argc, char *argv[])
int ret = app.exec(); int ret = app.exec();
// Clean up threads // Clean up threads
imageAcquisitionThread->exit(); threadManager.finishThreads();
imageAcquisitionThread->wait();
imageProcessingThread->exit();
imageProcessingThread->wait();
NetworkStreamThread->exit();
NetworkStreamThread->wait();
journalThread->exit();
journalThread->wait();
return ret; return ret;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment