Much more correct work with QThreadPool.

Use "volatile" keyword. Don't use method waitForDone() anymore.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-05-06 17:09:58 +03:00
parent 84661014e8
commit 44ef0936d9
5 changed files with 18 additions and 20 deletions

View File

@ -91,7 +91,7 @@ private:
VBank *bank;
int paperHeight;
int paperWidth;
bool stopGeneration;
volatile bool stopGeneration;
LayoutErrors state;
unsigned int shift;
bool rotate;

View File

@ -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<VPosition *> 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)
{

View File

@ -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<QGraphicsItem *> GetDetails() const;
@ -75,7 +75,7 @@ public:
private:
QSharedDataPointer<VLayoutPaperData> d;
bool AddToSheet(const VLayoutDetail &detail, bool &stop);
bool AddToSheet(const VLayoutDetail &detail, volatile bool &stop);
bool SaveResult(const VBestSquare &bestResult, const VLayoutDetail &detail);

View File

@ -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<VLayoutDetail>()), 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;

View File

@ -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<VLayoutDetail> 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;