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 += \
$${TOP}/src/PerfCounter.cpp \
$${TOP}/src/Calibration.cpp \
$${TOP}/src/settings.cpp \
$${TOP}/src/utils.cpp
$${TOP}/src/utils.cpp \
src/ThreadManager.cpp
HEADERS += \
$${TOP}/src/ImageProcessing.h \
......@@ -50,7 +51,8 @@ HEADERS += \
$${TOP}/else/blob_gen.h \
$${TOP}/else/canny_impl.h \
$${TOP}/else/filter_edges.h \
$${TOP}/else/find_best_edge.h
$${TOP}/else/find_best_edge.h \
src/ThreadManager.h
FORMS += \
$${TOP}/src/Gui.ui
......
......@@ -60,10 +60,11 @@ void ImageAcquisition::onThreadStarted(){
slaveThread = QtConcurrent::run(updateSlave, this);
}
void ImageAcquisition::onThreadFinished()
void ImageAcquisition::onThreadFinishing()
{
running = false;
slaveThread.waitForFinished();
qDebug() << QThread::currentThread()->objectName() << " done.";
}
ImageAcquisition::ImageAcquisition(QObject *parent) : QObject(parent),
......
......@@ -62,7 +62,7 @@ signals:
public slots:
void onThreadStarted();
void onThreadFinished();
void onThreadFinishing();
};
......
......@@ -14,8 +14,9 @@ void ImageProcessing::onThreadStarted(){
m_running = true;
}
void ImageProcessing::onThreadFinished()
void ImageProcessing::onThreadFinishing()
{
qDebug() << QThread::currentThread()->objectName() << " done.";
}
void ImageProcessing::project(JournalEntry &journalEntry)
......
#ifndef IMAGEPROCESSING_H
#define IMAGEPROCESSING_H
#include <qobject.h>
#include <QObject>
#include <QTime>
#include <QPoint>
#include <qdebug.h>
#include <QThread>
#include <QDebug>
#include <opencv/cv.h>
#include "Journal.h"
......@@ -36,7 +38,7 @@ public:
public slots:
void onThreadStarted();
void onThreadFinished();
void onThreadFinishing();
void processImage(JournalEntry journalEntry);
signals:
......
......@@ -130,8 +130,9 @@ void Journal::onThreadStarted()
{
}
void Journal::onThreadFinished()
void Journal::onThreadFinishing()
{
qDebug() << QThread::currentThread()->objectName() << " done.";
}
void Journal::setOutput()
......
......@@ -13,6 +13,7 @@
#include <QDir>
#include <QFileInfoList>
#include <QRegExp>
#include <QThread>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
......@@ -97,7 +98,7 @@ public slots:
void store(JournalEntry journalEntry);
void setRecording(bool val);
void onThreadStarted();
void onThreadFinished();
void onThreadFinishing();
signals:
void dumpCalibration(QString idxStr);
......
......@@ -22,7 +22,7 @@ void NetworkStream::onThreadStarted()
}
}
void NetworkStream::onThreadFinished()
void NetworkStream::onThreadFinishing()
{
if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
tcpServer->close();
......@@ -32,6 +32,7 @@ void NetworkStream::onThreadFinished()
} else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) {
delete socket; socket;
}
qDebug() << QThread::currentThread()->objectName() << " done.";
}
void NetworkStream::openSocket()
......
......@@ -21,7 +21,7 @@ public slots:
void closeSocket();
void push(JournalEntry journalEntry);
void onThreadStarted();
void onThreadFinished();
void onThreadFinishing();
void timerEvent(QTimerEvent *event);
private:
......
......@@ -7,6 +7,7 @@
#include "ImageAcquisition.h"
#include "ImageProcessing.h"
#include "NetworkStream.h"
#include "ThreadManager.h"
#include "Journal.h"
#include "Gui.h"
......@@ -14,22 +15,6 @@
#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[])
{
QApplication app(argc, argv);
......@@ -115,12 +100,13 @@ int main(int argc, char *argv[])
QThread::currentThread()->setObjectName("Gui");
// Set priorities
// Thread Management
ThreadManager threadManager;
QThread::currentThread()->setPriority(QThread::NormalPriority);
QThread *imageAcquisitionThread = runInOwnThread(imageAcquisition, "ImageAcquisitionThread", QThread::TimeCriticalPriority);
QThread *imageProcessingThread = runInOwnThread(imageProcessing, "ImageProcessingThread", QThread::TimeCriticalPriority);
QThread *NetworkStreamThread = runInOwnThread(networkStream, "NetworkStreamThread", QThread::TimeCriticalPriority);
QThread *journalThread = runInOwnThread(journal, "JournalThread", QThread::NormalPriority);
threadManager.newThread(imageAcquisition, "ImageAcquisitionThread", QThread::TimeCriticalPriority);
threadManager.newThread(imageProcessing, "ImageProcessingThread", QThread::TimeCriticalPriority);
threadManager.newThread(networkStream, "NetworkStreamThread", QThread::TimeCriticalPriority);
threadManager.newThread(journal, "JournalThread", QThread::NormalPriority);
// Ready to start
QRect screenGeometry = QApplication::desktop()->screenGeometry();
......@@ -134,17 +120,7 @@ int main(int argc, char *argv[])
int ret = app.exec();
// Clean up threads
imageAcquisitionThread->exit();
imageAcquisitionThread->wait();
imageProcessingThread->exit();
imageProcessingThread->wait();
NetworkStreamThread->exit();
NetworkStreamThread->wait();
journalThread->exit();
journalThread->wait();
threadManager.finishThreads();
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