C++ 我使用Qt的多线程应用程序有什么问题(错误SIGSEGV)
我是Qt的新手,我正在寻找Qt中的多线程。C++ 我使用Qt的多线程应用程序有什么问题(错误SIGSEGV),c++,multithreading,qt,C++,Multithreading,Qt,我是Qt的新手,我正在寻找Qt中的多线程。 正如我在中所学到的,我为两个线程定义了两个类: #include <QThread> #include <QMutex> class thread_a : public QThread { Q_OBJECT public: explicit thread_a(QObject *parent = 0); int counter; protected: void run(); }; 第二个线程类
正如我在中所学到的,我为两个线程定义了两个类:
#include <QThread>
#include <QMutex>
class thread_a : public QThread
{
Q_OBJECT
public:
explicit thread_a(QObject *parent = 0);
int counter;
protected:
void run();
};
第二个线程类相同,但在run()
方法中使用了计数器--
。然后我从
main.ccp
运行这两个线程。没问题。但当我在插槽上运行这两个线程时,出现了一个问题。标题为“Signal Received”(信号接收)的对话框,表示“由于接收到来自操作系统的信号,下级停止。信号名称:SIGSEGV,信号含义:分段故障”
怎么了
更新:这是我的位置:
public slots:
void run_threads(bool);
及
我通过以下方式将一个按钮连接到此插槽:
QObject::connect(ui->pushButton, SIGNAL(clicked(bool)),
this, SLOT(run_threads(bool)));
插槽创建线程类的两个实例,启动它们,然后退出。在这一点上,函数返回,实例超出范围,因此被销毁。您应该保留对实例的访问权,以便它们不会被破坏,并且可能会重用它们
class MainWindow {
//...other stuff
public slots:
void run_threads(bool);
private:
thread_a a;
thread_b b;
//...other stuff
};
及
请发布您用于“在插槽上运行两个线程”的代码。
QObject::connect(ui->pushButton, SIGNAL(clicked(bool)),
this, SLOT(run_threads(bool)));
class MainWindow {
//...other stuff
public slots:
void run_threads(bool);
private:
thread_a a;
thread_b b;
//...other stuff
};
void MainWindow::run_threads(bool bl)
{
if(!a.isRunning())
a.start();
if(!b.isRunning())
b.start();
}