From 44ef0936d9689bea7d28e6f2a61cc921aa1899f6 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 6 May 2015 17:09:58 +0300 Subject: [PATCH] Much more correct work with QThreadPool. Use "volatile" keyword. Don't use method waitForDone() anymore. --HG-- branch : develop --- src/libs/vlayout/vlayoutgenerator.h | 2 +- src/libs/vlayout/vlayoutpaper.cpp | 16 +++++++++------- src/libs/vlayout/vlayoutpaper.h | 4 ++-- src/libs/vlayout/vposition.cpp | 8 ++------ src/libs/vlayout/vposition.h | 8 ++++---- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/libs/vlayout/vlayoutgenerator.h b/src/libs/vlayout/vlayoutgenerator.h index f1b7d6124..71fb1974f 100644 --- a/src/libs/vlayout/vlayoutgenerator.h +++ b/src/libs/vlayout/vlayoutgenerator.h @@ -91,7 +91,7 @@ private: VBank *bank; int paperHeight; int paperWidth; - bool stopGeneration; + volatile bool stopGeneration; LayoutErrors state; unsigned int shift; bool rotate; diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp index f8c008517..369ac9b19 100644 --- a/src/libs/vlayout/vlayoutpaper.cpp +++ b/src/libs/vlayout/vlayoutpaper.cpp @@ -154,7 +154,7 @@ void VLayoutPaper::SetPaperIndex(quint32 index) } //--------------------------------------------------------------------------------------------------------------------- -bool VLayoutPaper::ArrangeDetail(const VLayoutDetail &detail, bool &stop) +bool VLayoutPaper::ArrangeDetail(const VLayoutDetail &detail, volatile bool &stop) { // First need set size of paper if (d->globalContour.GetHeight() <= 0 || d->globalContour.GetWidth() <= 0) @@ -179,15 +179,13 @@ int VLayoutPaper::Count() const } //--------------------------------------------------------------------------------------------------------------------- -bool VLayoutPaper::AddToSheet(const VLayoutDetail &detail, bool &stop) +bool VLayoutPaper::AddToSheet(const VLayoutDetail &detail, volatile bool &stop) { VBestSquare bestResult; QThreadPool *thread_pool = QThreadPool::globalInstance(); thread_pool->setExpiryTimeout(1000); QVector threads; - QCoreApplication::processEvents(); - for (int j=1; j <= d->globalContour.EdgesCount(); ++j) { for (int i=1; i<= detail.EdgesCount(); i++) @@ -209,9 +207,13 @@ bool VLayoutPaper::AddToSheet(const VLayoutDetail &detail, bool &stop) } } - thread_pool->waitForDone(); - - QCoreApplication::processEvents(); + // Wait for done + do + { + QCoreApplication::processEvents(); + QThread::msleep(250); + } + while(thread_pool->activeThreadCount() > 0 && not stop); if (stop) { diff --git a/src/libs/vlayout/vlayoutpaper.h b/src/libs/vlayout/vlayoutpaper.h index 763d15cc3..1fc409493 100644 --- a/src/libs/vlayout/vlayoutpaper.h +++ b/src/libs/vlayout/vlayoutpaper.h @@ -67,7 +67,7 @@ public: void SetPaperIndex(quint32 index); - bool ArrangeDetail(const VLayoutDetail &detail, bool &stop); + bool ArrangeDetail(const VLayoutDetail &detail, volatile bool &stop); int Count() const; QGraphicsRectItem *GetPaperItem(bool autoCrop) const; QList GetDetails() const; @@ -75,7 +75,7 @@ public: private: QSharedDataPointer d; - bool AddToSheet(const VLayoutDetail &detail, bool &stop); + bool AddToSheet(const VLayoutDetail &detail, volatile bool &stop); bool SaveResult(const VBestSquare &bestResult, const VLayoutDetail &detail); diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp index e05998e7f..7532799c6 100644 --- a/src/libs/vlayout/vposition.cpp +++ b/src/libs/vlayout/vposition.cpp @@ -46,8 +46,8 @@ #endif //--------------------------------------------------------------------------------------------------------------------- -VPosition::VPosition(const VContour &gContour, int j, const VLayoutDetail &detail, int i, bool *stop, bool rotate, - int rotationIncrease) +VPosition::VPosition(const VContour &gContour, int j, const VLayoutDetail &detail, int i, volatile bool *stop, + bool rotate, int rotationIncrease) :QRunnable(), bestResult(VBestSquare()), gContour(gContour), detail(detail), i(i), j(j), paperIndex(0), frame(0), detailsCount(0), details(QVector()), stop(stop), rotate(rotate), rotationIncrease(rotationIncrease) { @@ -65,8 +65,6 @@ void VPosition::run() return; } - QCoreApplication::processEvents(); - // We should use copy of the detail. VLayoutDetail workDetail = detail; @@ -654,8 +652,6 @@ void VPosition::Rotate(int increase) return; } - QCoreApplication::processEvents(); - // We should use copy of the detail. VLayoutDetail workDetail = detail; diff --git a/src/libs/vlayout/vposition.h b/src/libs/vlayout/vposition.h index aac2fdb7e..ba2461a79 100644 --- a/src/libs/vlayout/vposition.h +++ b/src/libs/vlayout/vposition.h @@ -46,12 +46,10 @@ class QPainterPath; class VPosition : public QRunnable { public: - VPosition(const VContour &gContour, int j, const VLayoutDetail &detail, int i, bool *stop, bool rotate, + VPosition(const VContour &gContour, int j, const VLayoutDetail &detail, int i, volatile bool *stop, bool rotate, int rotationIncrease); virtual ~VPosition(){} - virtual void run(); - quint32 getPaperIndex() const; void setPaperIndex(const quint32 &value); @@ -81,7 +79,7 @@ private: quint32 frame; quint32 detailsCount; QVector details; - bool *stop; + volatile bool *stop; bool rotate; int rotationIncrease; @@ -99,6 +97,8 @@ private: EdgeError = 2 }; + virtual void run(); + void SaveCandidate(VBestSquare &bestResult, const VLayoutDetail &detail, int globalI, int detJ, BestFrom type); bool CheckCombineEdges(VLayoutDetail &detail, int j, int &dEdge) const;