gpt4 book ai didi

c++ - QMutex with QThread - 质数搜索器

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:35 25 4
gpt4 key购买 nike

你好,我在用 qmutex 掌握 qthread 时遇到了问题——通常如何使用它——学习它我正在尝试创建限制为 8,000,000 的质数搜索器,但即使我使用给定的代码作为基础,我也失败了对于类似的例子——我真的不知道我误解了什么,但我创建了一个全局变量并将它传递给两个线程——在给定的代码中,一个线程递增变量,第二个线程进行计算——但是这不是好的解决方案 - 从长远来看不是 - 代码最多可以工作 8000 所以说我不确定我是否做对了。文档让我对如何操作知之甚少——代码示例不是很有帮助。

我试过:- 以代码示例为基础运行它- 在主线程中创建一个循环是什么破坏了程序- 过滤两个线程不会冲突的数字

这是实际的代码:计算object.h

#include <QObject>
#include <QThread>

class CalculateObject : public QThread
{
Q_OBJECT
public:
CalculateObject(QMutex *mu, int *nu);
void run();
bool isPrime(unsigned int);
QMutex *mutex;
int *counter;
bool checker;
QString name;
signals:
void sendResult(int);
void done();
};

计算object.cpp

#include "calculateobject.h"
#include <QtMath>


const int LIMIT = 8000000;

CalculateObject::CalculateObject(QMutex *mu, int *nu)
{
counter = nu;
mutex = mu;
checker = false;
}
bool CalculateObject::isPrime(unsigned int number)
{
if(number > 3)
{
if (number % 2 == 0)
{
return false;
}
const unsigned int MAX = (unsigned int)sqrt(number) + 1;
for (int i = 3;i < MAX; i += 2) {
if((number % i) == 0)
{
return false;
}
}
return true;
}
if(number < 2)
{
return false;
}
return true;
}
void CalculateObject::run()
{
int result = 0;
while(*counter < 8000)
{
QThread::usleep(5);
if(this->name == "first")
{
mutex->lock();
*counter += 1;
qDebug() << *counter;
mutex->unlock();
}
else {
mutex->lock();
checker = isPrime(*counter);
mutex->unlock();
if(checker == true)
{
result = 1;
emit(sendResult(result));
}

}
}
emit(done());
}

计算器控件.h

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include "calculateobject.h"
#include <QThread>

class CalculatorWidget : public QWidget
{
Q_OBJECT

public:
explicit CalculatorWidget(QWidget *parent = nullptr);
~CalculatorWidget();
CalculateObject *object, *object2;
public slots:
void readResults(int counter);
void startThread();
void finishThread();
void endMessage();
void controller();
private:
int reader;
Ui::CalculatorWidget *ui;
QPushButton *start;
QLabel *resultReader;
QThread *newProcess;
QThread *newerProcess;
signals:
void end();

};

计算器控件.cpp

#include "calculatorwidget.h"
#include "ui_calculatorwidget.h"
#include <QMessageBox>

int number = 0;
QMutex aMutex;

CalculatorWidget::CalculatorWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::CalculatorWidget)
{
ui->setupUi(this);
start = ui->start;
resultReader = ui->result;
newProcess = new QThread;
newerProcess = new QThread;
reader = 0;
connect(start, &QPushButton::clicked, this, &CalculatorWidget::startThread);
}
void CalculatorWidget::controller()
{
static int control = 0;
control++;
if(control == 2)
{
emit(end());
}
}

CalculatorWidget::~CalculatorWidget()
{
delete ui;
}
void CalculatorWidget::readResults(int counter)
{
reader += counter;
}
void CalculatorWidget::startThread()
{
int *count = &number;
QMutex *mutex = &aMutex;
object = new CalculateObject(mutex, count);
object2 = new CalculateObject(mutex, count);
connect(object, &CalculateObject::sendResult, this, &CalculatorWidget::readResults);
connect(object2, &CalculateObject::sendResult, this, &CalculatorWidget::readResults);
connect(object, &CalculateObject::done, this, &CalculatorWidget::controller);
connect(object2, &CalculateObject::done, this, &CalculatorWidget::controller);
connect(this, &CalculatorWidget::end, this, &CalculatorWidget::finishThread);
object->name = "first";
object2->name = "second";
object->start();
object2->start();

}
void CalculatorWidget::finishThread()
{
newProcess->exit();
newProcess->wait(3);
newerProcess->wait(3);
newerProcess->exit();
endMessage();
}
void CalculatorWidget::endMessage()
{
QMessageBox msg;
msg.setText("The calculation Process is done" + QString::number(reader));
msg.exec();
}

主要.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CalculatorWidget widget;
widget.show();
return a.exec();
}


用户界面文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CalculatorWidget</class>
<widget class="QWidget" name="CalculatorWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>This program calculates how many prime numbers </string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>are in de spectrum from 0 to 8000000</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="start">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="result">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

我希望我的代码能显示 0 到 8,000,000 范围内有多少素数,但程序挂起或崩溃

最佳答案

我真的不知道,你的问题到底是什么,但你应该尝试在 run() 函数的开头使用 QMutexLocker。试试你的程序是否可以安全运行。

您还应该记住,读取共享变量也必须受到互斥锁的保护。另见帖子 QMutex needed to read variable .

void CalculateObject::run()
{
QMutexLocker lock(mutex);
int result = 0;
while(*counter < 8000)
{
//QThread::usleep(5);
if(this->name == "first")
{
*counter += 1;
qDebug() << *counter;
}
else {
checker = isPrime(*counter);
if(checker == true)
{
result = 1;
emit(sendResult(result));
}

}
}
emit(done());
}

如果这解决了您的问题,请报告。

关于c++ - QMutex with QThread - 质数搜索器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451986/

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