bool isPrime = true ;
for ( int i = 2; i <= currentDummy/2; isPrime &= (currentDummy%i) != 0, ++i);
* This source code was highlighted with Source Code Highlighter .
//workerthread.h
#ifndef WORKERTHREAD_H
#define WORKERTHREAD_H
#include <QThread>
class WorkerThread : public QThread
{
Q_OBJECT
public :
explicit WorkerThread( int start, int end, int step, QObject *parent = 0);
signals:
void updateProcess( int value );
public slots:
protected :
void run();
private :
int rangeStart;
int rangeEnd;
int rangeStep;
};
#endif // WORKERTHREAD_H
//workerthread.cpp
#include "workerthread.h"
WorkerThread::WorkerThread( int start, int end, int step, QObject *parent) :
QThread(parent), rangeStart(start), rangeEnd(end), rangeStep(step)
{
}
void WorkerThread::run()
{
int currentDummy = rangeStart;
int currentProcess = 0;
while (currentDummy <= rangeEnd)
{
bool isPrime = true ;
for ( int i = 2; i <= currentDummy/2; isPrime &= (currentDummy%i) != 0, ++i)
{
//Method 2. Slower, but more responsive UI
// if (!(i%500))
// yieldCurrentThread();
}
if (!(currentProcess%1000))
{
emit updateProcess(currentProcess);
}
currentDummy += rangeStep;
++currentProcess;
//Method 1. Faster, but with UI stucks
yieldCurrentThread();
}
emit updateProcess(currentProcess);
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/85194/