c++ - QThread:线程仍在运行时被销毁,QMutex 被销毁

标签 c++ multithreading qt qthread

我正在尝试向我的 Qt 应用程序添加多个线程,但是当它执行这个线程时,程序就崩溃了,我得到了一个错误

QThread: Destroyed while thread is still running

QMutex: destroying locked mutex

我理解错误信息,只是不知道如何修复它。我的代码如下。

标题

class Worker : public QObject
{
    Q_OBJECT
private slots:
    void onTimeout()
    {
        qDebug()<<"Worker::onTimeout get called from?: "<<QThread::currentThreadId();
    }
};

class Thread : public QThread
{
    Q_OBJECT

private:
    void run()
    {
        qDebug()<<"From work thread: "<<currentThreadId();
        QTimer timer;
        Worker worker;
        connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
        timer.start(1000);
        exec();
    }
};

登录.cpp

    void Login::on_pushButton_clicked()
{
    QSqlQuery query;
    QString Username = ui->Username_lineEdit->text();
    QString Password = ui->Password_lineEdit->text();
    query.prepare("SELECT * FROM Program_account WHERE Login = '"+ Username +"' AND Password = '"+ Password +"'");
    if(!query.exec())
    {
        qDebug() << "SQL QUERY Login:" << query.executedQuery();
        qDebug() << "SQL ERROR Login:" << query.lastError();
    }
    else if(!query.first())
    {
        tries++;
        int x = 10 - tries;
        ui->label->setText("Incorrect Username or Password " + QString::number(x) + " tries until timeout");
    }
    else
    {
        QSqlQuery Account_Type_Query("SELECT Account_Type FROM Program_account WHERE Login = '"+ Username +"' AND Password = '"+ Password +"'");
        while(Account_Type_Query.next())
        {
            Account_Type = Account_Type_Query.value(0).toInt();
        }
        tries = 0;
        static Home *home = new Home;
        home->show();
        close();
    }
    if(tries == 10)
    {
        Thread t;
        t.start();
        ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
        ui->pushButton->hide();
        QTimer::singleShot(600000, ui->pushButton, SLOT(show()));
        tries = 0;
        ui->label->setText("");
    }

解决此问题的正确方法是什么。非常感谢所有帮助。

谢谢

更新: 尝试过 线程类:公共(public) QThread { Q_OBJECT

private:
    void run()
    {
        while(QThread::wait())
        {
        qDebug()<<"From work thread: "<<currentThreadId();
        QTimer timer;
        Worker worker;
        connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
        timer.start(1000);
        exec();
        }
        QThread::quit();
    }
};

但仍然收到相同的错误

最佳答案

从 QThread 继承是这里的一个问题,我怀疑 Worker 对象不具有您认为它具有的线程亲和性并且在主线程上运行。

崩溃的原因是你在栈上创建了Thread实例,

if(tries == 10)
{
    Thread t; // NOTE THIS IS ON THE STACK
    t.start();
    ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
    ui->pushButton->hide();
    QTimer::singleShot(600000, ui->pushButton, SLOT(show()));
    tries = 0;
    ui->label->setText("");
}

Thread 实例在超出范围时被销毁。

无论如何,我强烈建议遵循@Thomas 的建议并坚持 Maya 使用线程的方式,而不需要继承 QThread。

关于c++ - QThread:线程仍在运行时被销毁,QMutex 被销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282198/

相关文章:

c++ - 如何创建可调整大小和固定大小容器的变体

c++ - 创建模板嵌套类实例的语法?

c++ - 错误使用条件变量

c++ - Boost 线程概念/问题

c++ - 时钟()与getsystemtime()

c++ - 为什么 qt/qmake 找不到我的共享库

c++ - 将 JSON 日志记录宏转换为模板函数...代码中需要的参数名称

c++ - 返回(大)对象时复制开销?

qt - QML:带有 "Back"按钮的 ListView 和工具栏

c++ - QSet:qHash 和自定义类错误