-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
99 lines (83 loc) · 2.91 KB
/
Copy pathmainwindow.cpp
File metadata and controls
99 lines (83 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "collatzthread.h"
#include <QThreadPool>
#include <QTextEdit>
#include <QDebug>
#include <shared_mutex>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow), maxsteps(0), maxpos(0)
{
ui->setupUi(this);
connect(ui->buttonStart, &QPushButton::clicked, this, &MainWindow::onButtonStartClicked);
connect(ui->buttonStop, &QPushButton::clicked, this, &MainWindow::onButtonStopClicked);
connect(ui->horizontalSlider, &QSlider::valueChanged, this, &MainWindow::onSliderValueChanged);
connect(ui->buttonExit, &QPushButton::clicked, this, &QWidget::close);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onButtonStartClicked()
{
ui->result->clear();
ui->buttonStart->setEnabled(false);
ui->buttonStop->setEnabled(true);
ui->spinBox->setEnabled(false);
ui->horizontalSlider->setEnabled(false);
int64_t maxNumber = ui->spinBox->value();
const uint64_t maxLimit = UINT64_MAX;
if (maxNumber > maxLimit) {
ui->result->setText(QString("Maximum number cannot exceed %1").arg(maxLimit));
return;
}
int numThreads = ui->horizontalSlider->value();
int rangePerThread = maxNumber / numThreads;
int remainingRange = maxNumber % numThreads;
int start = 1;
for (int i = 0; i < numThreads; ++i) {
int end = start + rangePerThread - 1;
if (i < remainingRange) {
end++;
}
std::unique_lock<std::shared_mutex> lock(sharedMutex);
CollatzThread *task = new CollatzThread(start, end, &maxsteps, &maxpos);
task->setAutoDelete(true);
connect(task, &CollatzThread::resultReady, this, &MainWindow::onResultReady);
QThreadPool::globalInstance()->start(task);
start = end + 1;
}
ui->buttonStart->setEnabled(true);
ui->buttonStop->setEnabled(true);
ui->spinBox->setEnabled(true);
ui->horizontalSlider->setEnabled(true);
}
void MainWindow::onButtonStopClicked()
{
ui->buttonStart->setEnabled(true);
ui->buttonStop->setEnabled(false);
ui->spinBox->setEnabled(true);
ui->horizontalSlider->setEnabled(true);
ui->result->setText(QString("The program was stopped."));
}
void MainWindow::onSliderValueChanged(int value)
{
ui->sliderValue->setText(QString::number(value));
}
void MainWindow::onResultReady(int number, int chain, long long time)
{
{
std::unique_lock<std::shared_mutex> lock(sharedMutex);
if (chain > maxsteps.load()) {
maxsteps.store(chain);
maxpos.store(number);
}
}
{
std::shared_lock<std::shared_mutex> lock(sharedMutex);
ui->result->setText(QString("Number with the largest chain: %1 with %2 steps\nTime: %3 milliseconds")
.arg(maxpos.load())
.arg(maxsteps.load())
.arg(time));
}
}