Do not use "volatile" to sync threads.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2017-02-22 14:04:47 +02:00
parent a0380f4d24
commit e3d6b32aec
6 changed files with 52 additions and 24 deletions

View file

@ -39,10 +39,24 @@
//---------------------------------------------------------------------------------------------------------------------
VLayoutGenerator::VLayoutGenerator(QObject *parent)
:QObject(parent), papers(QVector<VLayoutPaper>()), bank(new VBank()), paperHeight(0), paperWidth(0), margins(),
usePrinterFields(true),stopGeneration(false), state(LayoutErrors::NoError), shift(0), rotate(true),
rotationIncrease(180), autoCrop(false), saveLength(false), unitePages(false), stripOptimizationEnabled(false),
multiplier(1), stripOptimization(false)
: QObject(parent),
papers(),
bank(new VBank()),
paperHeight(0),
paperWidth(0),
margins(),
usePrinterFields(true),
stopGeneration(false),
state(LayoutErrors::NoError),
shift(0),
rotate(true),
rotationIncrease(180),
autoCrop(false),
saveLength(false),
unitePages(false),
stripOptimizationEnabled(false),
multiplier(1),
stripOptimization(false)
{}
//---------------------------------------------------------------------------------------------------------------------
@ -79,7 +93,7 @@ int VLayoutGenerator::DetailsCount()
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::Generate()
{
stopGeneration = false;
stopGeneration.store(false);
papers.clear();
state = LayoutErrors::NoError;
@ -110,7 +124,7 @@ void VLayoutGenerator::Generate()
while (bank->AllDetailsCount() > 0)
{
if (stopGeneration)
if (stopGeneration.load())
{
break;
}
@ -135,13 +149,13 @@ void VLayoutGenerator::Generate()
bank->NotArranged(index);
}
if (stopGeneration)
if (stopGeneration.load())
{
break;
}
} while(bank->LeftArrange() > 0);
if (stopGeneration)
if (stopGeneration.load())
{
break;
}
@ -209,7 +223,7 @@ QList<QList<QGraphicsItem *> > VLayoutGenerator::GetAllDetails() const
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::Abort()
{
stopGeneration = true;
stopGeneration.store(true);
state = LayoutErrors::ProcessStoped;
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
QThreadPool::globalInstance()->clear();

View file

@ -37,6 +37,7 @@
#include <QVector>
#include <QtGlobal>
#include <memory>
#include <atomic>
#include "vbank.h"
#include "vlayoutdef.h"
@ -122,7 +123,7 @@ private:
qreal paperWidth;
QMarginsF margins;
bool usePrinterFields;
volatile bool stopGeneration;
std::atomic_bool stopGeneration;
LayoutErrors state;
quint32 shift;
bool rotate;

View file

@ -183,7 +183,7 @@ void VLayoutPaper::SetPaperIndex(quint32 index)
}
//---------------------------------------------------------------------------------------------------------------------
bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, volatile bool &stop)
bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &stop)
{
// First need set size of paper
if (d->globalContour.GetHeight() <= 0 || d->globalContour.GetWidth() <= 0)
@ -219,7 +219,7 @@ int VLayoutPaper::Count() const
}
//---------------------------------------------------------------------------------------------------------------------
bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, volatile bool &stop)
bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop)
{
VBestSquare bestResult(d->globalContour.GetSize(), d->saveLength);
QThreadPool *thread_pool = QThreadPool::globalInstance();
@ -266,9 +266,9 @@ bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, volatile bool &stop)
QCoreApplication::processEvents();
QThread::msleep(250);
}
while(thread_pool->activeThreadCount() > 0 && not stop);
while(thread_pool->activeThreadCount() > 0 && not stop.load());
if (stop)
if (stop.load())
{
qDeleteAll(threads.begin(), threads.end());
threads.clear();

View file

@ -33,6 +33,7 @@
#include <QSharedDataPointer>
#include <QTypeInfo>
#include <QtGlobal>
#include <atomic>
#include "vlayoutdef.h"
@ -77,7 +78,7 @@ public:
void SetPaperIndex(quint32 index);
bool ArrangeDetail(const VLayoutPiece &detail, volatile bool &stop);
bool ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &stop);
int Count() const;
QGraphicsRectItem *GetPaperItem(bool autoCrop) const Q_REQUIRED_RESULT;
QList<QGraphicsItem *> GetItemDetails() const Q_REQUIRED_RESULT;
@ -90,7 +91,7 @@ public:
private:
QSharedDataPointer<VLayoutPaperData> d;
bool AddToSheet(const VLayoutPiece &detail, volatile bool &stop);
bool AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop);
bool SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail);

View file

@ -50,11 +50,22 @@
#include "../vmisc/vmath.h"
//---------------------------------------------------------------------------------------------------------------------
VPosition::VPosition(const VContour &gContour, int j, const VLayoutPiece &detail, int i, volatile bool *stop,
VPosition::VPosition(const VContour &gContour, int j, const VLayoutPiece &detail, int i, std::atomic_bool *stop,
bool rotate, int rotationIncrease, bool saveLength)
:QRunnable(), bestResult(VBestSquare(gContour.GetSize(), saveLength)), gContour(gContour), detail(detail), i(i),
j(j), paperIndex(0), frame(0), detailsCount(0), details(QVector<VLayoutPiece>()), stop(stop), rotate(rotate),
rotationIncrease(rotationIncrease), angle_between(0)
: QRunnable(),
bestResult(VBestSquare(gContour.GetSize(), saveLength)),
gContour(gContour),
detail(detail),
i(i),
j(j),
paperIndex(0),
frame(0),
detailsCount(0),
details(),
stop(stop),
rotate(rotate),
rotationIncrease(rotationIncrease),
angle_between(0)
{
if ((rotationIncrease >= 1 && rotationIncrease <= 180 && 360 % rotationIncrease == 0) == false)
{
@ -65,7 +76,7 @@ VPosition::VPosition(const VContour &gContour, int j, const VLayoutPiece &detail
//---------------------------------------------------------------------------------------------------------------------
void VPosition::run()
{
if (*stop)
if (stop->load())
{
return;
}
@ -466,7 +477,7 @@ void VPosition::Rotate(int increase)
}
for (int angle = startAngle; angle < 360; angle = angle+increase)
{
if (*stop)
if (stop->load())
{
return;
}

View file

@ -33,6 +33,7 @@
#include <QRunnable>
#include <QVector>
#include <QtGlobal>
#include <atomic>
#include "vbestsquare.h"
#include "vcontour.h"
@ -42,7 +43,7 @@
class VPosition : public QRunnable
{
public:
VPosition(const VContour &gContour, int j, const VLayoutPiece &detail, int i, volatile bool *stop, bool rotate,
VPosition(const VContour &gContour, int j, const VLayoutPiece &detail, int i, std::atomic_bool *stop, bool rotate,
int rotationIncrease, bool saveLength);
virtual ~VPosition() Q_DECL_OVERRIDE{}
@ -75,7 +76,7 @@ private:
quint32 frame;
quint32 detailsCount;
QVector<VLayoutPiece> details;
volatile bool *stop;
std::atomic_bool *stop;
bool rotate;
int rotationIncrease;
/**