QMutex with QThread - prime number sekeer



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

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

这是实际代码: 计算对象。

#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();
};

计算对象.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 的频谱中有多少个素数,但程序正在挂掉或压碎

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

您还应该记住,读取共享变量也必须受到互斥锁的保护。另请参阅QMutex需要读取变量的帖子。

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());
}

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

相关内容

  • 没有找到相关文章

最新更新