gpt4 book ai didi

c++ - 线程只执行一次

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:37 27 4
gpt4 key购买 nike

我尝试实现这个:当应用程序启动时,我需要创建多个线程,这些线程将使用相同的 QDialog 窗口从用户那里获取消息。当线程启动时,它会要求用户输入,如果按下按钮 OK,它会将消息打印到控制台。我不明白为什么,但我只得到一次对话框窗口,之后它会向控制台打印一条消息,应用程序完成。

我是这样描述对话窗口的:

#include <QtWidgets>

class MyDialog : public QDialog
{
Q_OBJECT
public:
QWaitCondition* condition;

explicit MyDialog(QWidget *parent = 0);

signals:
void got_message(QString);
public slots:
void show_message_input();
void show_message();
private:
QLabel* message_label;
QVBoxLayout* vbox;
QHBoxLayout* hbox;
QLineEdit* message_input;
QDialogButtonBox* dialog_buttons;

};

MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
setModal(true);

message_label = new QLabel("Message");
message_input = new QLineEdit();

dialog_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

hbox = new QHBoxLayout();
hbox->addWidget(message_label);
hbox->addWidget(message_input);

vbox = new QVBoxLayout();
vbox->addLayout(hbox);
vbox->addWidget(dialog_buttons);

setLayout(vbox);

connect(dialog_buttons, SIGNAL(accepted()), this, SLOT(accept()));
connect(dialog_buttons, SIGNAL(rejected()), this, SLOT(reject()));

condition = new QWaitCondition();
}

void MyDialog::show_message_input()
{
int result = this->exec();
if (result == QDialog::Accepted)
{
emit got_message(message_input->text());
condition->wakeAll();
}
}

这是 MyThread 类:

class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int id, MyDialog* window, QObject *parent = 0);

signals:
void show_input();
public slots:
void print_message(QString);
private:
static QMutex mutex;
static QMutex mutex2;
MyDialog* window;
int id;
void run();
void get_captcha_value();
};

QMutex MyThread::mutex;
QMutex MyThread::mutex2;

MyThread::MyThread(int id, MyDialog* window, QObject *parent) :
QThread(parent)
{
this->id = id;
this->window = window;

connect(this, SIGNAL(show_input()), this->window, SLOT(show_message_input()));
}

void MyThread::get_captcha_value()
{
QMutexLocker lock(&mutex);
connect(this->window, SIGNAL(got_message(QString)), SLOT(print_message(QString)));
emit show_input();
mutex2.lock();
window->condition->wait(&mutex2);
mutex2.unlock();
}

void MyThread::run()
{
mutex.lock();
qDebug() << "Starting thread " << id;
mutex.unlock();
get_captcha_value();
mutex.lock();
qDebug() << "Finishing thread " << id;
mutex.unlock();
}

void MyThread::print_message(QString message)
{
qDebug() << message;
QObject::disconnect(this, SLOT(print_message(QString)));
}

main函数:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog* window = new MyDialog();
QList<MyThread*> threads;
for(int i = 0; i < 5; i++)
{
MyThread* thread = new MyThread(i, window);
threads << thread;
thread->start();
}
return a.exec();
}

最佳答案

您遇到的第一个问题是您继承自 QThread。除非您想重写 Qt 处理线程的方式,you're doing it wrong! .

您需要做的是拥有一个继承自 QObject 的类,并将实例移动到新线程。继承 QThread 的主要问题是它会导致线程亲和性(一个对象在哪个线程上运行)的混淆。

此外,创建比处理器内核更多的线程只是一种资源浪费。

我建议你read this article关于如何使用 Qt 线程并停止从 QThread 继承。

最后,QMutex的用途是保护多个线程同时访问同一个数据。您应该能够在您显示的代码中删除所有这些。 Qt 中的首选方法是从一个线程发出信号并由另一个线程上的槽接收数据。

关于c++ - 线程只执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21628483/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com