GuiHanoi
23rd February 2016, 10:23
Hi everyone,
I'm new on this forum. I'm using Qt4.8.6 for its GUI performances but now I want to use QThreads properly. I saw on the web different ways to declare and use them.
- redefinition of methods run() of Object inheriting of QThread class => Wrong way
- definition of a Worker and worker->moveToThread (QThread*) => Right way ??? (https://forum.qt.io/topic/14378/my-tutorial-on-how-to-properly-use-qthreads)
I developed a small project with these two methods to compare them. threadA which displays "A" and threadB which displays "B". To start/stop each threads, there are buttons. Both of these methods work (displaying AAAAA when AButton pushed, BBBBBBB when BButton pushed and ABABABABABA when both are pushed...
BUT if I follow steps of the second method (Correct use), I must connect :
connect(thread, SIGNAL(started()), worker, SLOT(process())); OK
connect(worker, SIGNAL(finished()), thread, SLOT(quit())); OK
but when I add these two connections, I have : Violation of Ox0FFFFFFFF emplacement Mutex....
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
How can I do to make this error disappear ?
Other question, in my code when i'm displaying threadID, it always displays 00000000159 (for example), for both threads ! I think I'm missing something.... Thread must NOT have the same ID ???? principles ??? (same problem in the two methods)
this is my code, without hpp files :
-------------------------------------------
Worker.cpp
Worker::Worker() { stopped = false;} // attribute of Worker instance
void Worker::process()
{
while (!stopped)
std::cout << messageStr.toStdString(); // attribute of Worker instance
emit finished();
}
void Worker::stop() { stopped = true;}
void Worker::start() {stopped = false;}
---------------------------------------------
ThreadDialog.cpp
ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
{
workerA = new Worker();
workerB = new Worker();
threadAButton = new QPushButton(tr("Start A"));
threadBButton = new QPushButton(tr("Start B"));
quitButton = new QPushButton(tr("Quit"));
quitButton->setDefault(true);
threadA = new QThread;
workerA->setMessage("A ");
workerA->moveToThread(threadA);
threadB = new QThread;
workerB->setMessage("B ");
workerB->moveToThread(threadB);
connect(threadA, SIGNAL(started()), workerA, SLOT(process()));
connect(workerA, SIGNAL(finished()), threadA, SLOT(quit()));
//connect(workerA, SIGNAL(finished()), workerA, SLOT(deleteLater()));
//connect(threadA, SIGNAL(finished()), threadA, SLOT(deleteLater()));
connect(threadB, SIGNAL(started()), workerB, SLOT(process()));
connect(workerB, SIGNAL(finished()), threadB, SLOT(quit()));
//connect(workerB, SIGNAL(finished()), workerB, SLOT(deleteLater()));
//connect(threadB, SIGNAL(finished()), threadB, SLOT(deleteLater()));
connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
.... // displayLayout
}
void ThreadDialog::startOrStopThreadA()
{
if (threadA->isRunning())
{
workerA->stop();
threadAButton->setText(tr("Start A"));
}
else
{
std::cout << threadA->currentThreadId() << std::endl;
workerA->start();
threadA->start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB->isRunning())
{
workerB->stop();
threadBButton->setText(tr("Start B"));
}
else
{
std::cout << threadB->currentThreadId() << std::endl;
workerB->start();
threadB->start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
workerA->stop();
workerB->stop();
threadA->wait();
threadB->wait();
threadA->exit();
threadB->exit();
event->accept();
}
Please Help me to understand QThread features... :(
Thank you
GuiHanoi
I'm new on this forum. I'm using Qt4.8.6 for its GUI performances but now I want to use QThreads properly. I saw on the web different ways to declare and use them.
- redefinition of methods run() of Object inheriting of QThread class => Wrong way
- definition of a Worker and worker->moveToThread (QThread*) => Right way ??? (https://forum.qt.io/topic/14378/my-tutorial-on-how-to-properly-use-qthreads)
I developed a small project with these two methods to compare them. threadA which displays "A" and threadB which displays "B". To start/stop each threads, there are buttons. Both of these methods work (displaying AAAAA when AButton pushed, BBBBBBB when BButton pushed and ABABABABABA when both are pushed...
BUT if I follow steps of the second method (Correct use), I must connect :
connect(thread, SIGNAL(started()), worker, SLOT(process())); OK
connect(worker, SIGNAL(finished()), thread, SLOT(quit())); OK
but when I add these two connections, I have : Violation of Ox0FFFFFFFF emplacement Mutex....
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
How can I do to make this error disappear ?
Other question, in my code when i'm displaying threadID, it always displays 00000000159 (for example), for both threads ! I think I'm missing something.... Thread must NOT have the same ID ???? principles ??? (same problem in the two methods)
this is my code, without hpp files :
-------------------------------------------
Worker.cpp
Worker::Worker() { stopped = false;} // attribute of Worker instance
void Worker::process()
{
while (!stopped)
std::cout << messageStr.toStdString(); // attribute of Worker instance
emit finished();
}
void Worker::stop() { stopped = true;}
void Worker::start() {stopped = false;}
---------------------------------------------
ThreadDialog.cpp
ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
{
workerA = new Worker();
workerB = new Worker();
threadAButton = new QPushButton(tr("Start A"));
threadBButton = new QPushButton(tr("Start B"));
quitButton = new QPushButton(tr("Quit"));
quitButton->setDefault(true);
threadA = new QThread;
workerA->setMessage("A ");
workerA->moveToThread(threadA);
threadB = new QThread;
workerB->setMessage("B ");
workerB->moveToThread(threadB);
connect(threadA, SIGNAL(started()), workerA, SLOT(process()));
connect(workerA, SIGNAL(finished()), threadA, SLOT(quit()));
//connect(workerA, SIGNAL(finished()), workerA, SLOT(deleteLater()));
//connect(threadA, SIGNAL(finished()), threadA, SLOT(deleteLater()));
connect(threadB, SIGNAL(started()), workerB, SLOT(process()));
connect(workerB, SIGNAL(finished()), threadB, SLOT(quit()));
//connect(workerB, SIGNAL(finished()), workerB, SLOT(deleteLater()));
//connect(threadB, SIGNAL(finished()), threadB, SLOT(deleteLater()));
connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
.... // displayLayout
}
void ThreadDialog::startOrStopThreadA()
{
if (threadA->isRunning())
{
workerA->stop();
threadAButton->setText(tr("Start A"));
}
else
{
std::cout << threadA->currentThreadId() << std::endl;
workerA->start();
threadA->start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB->isRunning())
{
workerB->stop();
threadBButton->setText(tr("Start B"));
}
else
{
std::cout << threadB->currentThreadId() << std::endl;
workerB->start();
threadB->start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
workerA->stop();
workerB->stop();
threadA->wait();
threadB->wait();
threadA->exit();
threadB->exit();
event->accept();
}
Please Help me to understand QThread features... :(
Thank you
GuiHanoi