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

View File

@ -37,6 +37,7 @@
#include <QVector> #include <QVector>
#include <QtGlobal> #include <QtGlobal>
#include <memory> #include <memory>
#include <atomic>
#include "vbank.h" #include "vbank.h"
#include "vlayoutdef.h" #include "vlayoutdef.h"
@ -122,7 +123,7 @@ private:
qreal paperWidth; qreal paperWidth;
QMarginsF margins; QMarginsF margins;
bool usePrinterFields; bool usePrinterFields;
volatile bool stopGeneration; std::atomic_bool stopGeneration;
LayoutErrors state; LayoutErrors state;
quint32 shift; quint32 shift;
bool rotate; 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 // First need set size of paper
if (d->globalContour.GetHeight() <= 0 || d->globalContour.GetWidth() <= 0) 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); VBestSquare bestResult(d->globalContour.GetSize(), d->saveLength);
QThreadPool *thread_pool = QThreadPool::globalInstance(); QThreadPool *thread_pool = QThreadPool::globalInstance();
@ -266,9 +266,9 @@ bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, volatile bool &stop)
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QThread::msleep(250); 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()); qDeleteAll(threads.begin(), threads.end());
threads.clear(); threads.clear();

View File

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

View File

@ -50,11 +50,22 @@
#include "../vmisc/vmath.h" #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) bool rotate, int rotationIncrease, bool saveLength)
:QRunnable(), bestResult(VBestSquare(gContour.GetSize(), saveLength)), gContour(gContour), detail(detail), i(i), : QRunnable(),
j(j), paperIndex(0), frame(0), detailsCount(0), details(QVector<VLayoutPiece>()), stop(stop), rotate(rotate), bestResult(VBestSquare(gContour.GetSize(), saveLength)),
rotationIncrease(rotationIncrease), angle_between(0) 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) 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() void VPosition::run()
{ {
if (*stop) if (stop->load())
{ {
return; return;
} }
@ -466,7 +477,7 @@ void VPosition::Rotate(int increase)
} }
for (int angle = startAngle; angle < 360; angle = angle+increase) for (int angle = startAngle; angle < 360; angle = angle+increase)
{ {
if (*stop) if (stop->load())
{ {
return; return;
} }

View File

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