connect(thread, SIGNAL(started()), worker, SLOT(process())); OK
connect(worker, SIGNAL(finished()), thread, SLOT(quit())); OK
connect(thread, SIGNAL(started()), worker, SLOT(process())); OK
connect(worker, SIGNAL(finished()), thread, SLOT(quit())); OK
To copy to clipboard, switch view to plain text mode
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()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
To copy to clipboard, switch view to plain text mode
How can I do to make this error disappear ?
{
workerA = new Worker();
workerB = new Worker();
quitButton->setDefault(true);
workerA->setMessage("A ");
workerA->moveToThread(threadA);
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"));
}
}
{
workerA->stop();
workerB->stop();
threadA->wait();
threadB->wait();
threadA->exit();
threadB->exit();
event->accept();
}
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();
}
To copy to clipboard, switch view to plain text mode
Please Help me to understand QThread features...
Bookmarks