diff options
| author | 2014-11-17 07:08:49 +0100 | |
|---|---|---|
| committer | 2014-11-17 07:08:49 +0100 | |
| commit | 207f22bc4edd913e9e8c279eeba10caafa8df16b (patch) | |
| tree | ea1560500d6afddeae3ac98b6a5f84f9ba7422de /node/dicerollernode.cpp | |
| parent | 4c82c8a05f870ab201bd1a91e0e1230de46e2565 (diff) | |
| download | OneRoll-207f22bc4edd913e9e8c279eeba10caafa8df16b.tar.gz OneRoll-207f22bc4edd913e9e8c279eeba10caafa8df16b.zip | |
Add multithreading support but it seems slower. It has been disable.
Diffstat (limited to 'node/dicerollernode.cpp')
| -rw-r--r-- | node/dicerollernode.cpp | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 735aacb..27750df 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -2,13 +2,40 @@ #include "die.h" - +#include <QThread> +#include <QThreadPool> #include <QDebug> +#include <QTime> + + +DiceRoller::DiceRoller(QMutex* mutex,DiceResult* diceResult,int faces,int count) + : m_mutex(mutex),m_sharedDiceResult(diceResult),m_faces(faces),m_diceCount(count) +{ + +} + +void DiceRoller::run() +{ + for(quint64 i=0; i < m_diceCount ; ++i) + { + Die* die = new Die(); + die->setFaces(m_faces); + die->roll(); + m_mutex->lock(); + m_sharedDiceResult->insertResult(die); + m_mutex->unlock(); + } +} + + +////////////////////////////////////////////////// +/// \brief DiceRollerNode::DiceRollerNode +////////////////////////////////////////////////// DiceRollerNode::DiceRollerNode(quint64 faces) : m_faces(faces),m_myDiceResult(new DiceResult()) { - + m_mutex=new QMutex(); m_result=m_myDiceResult; } @@ -30,12 +57,40 @@ void DiceRollerNode::run(ExecutionNode* previous) die->roll(); m_myDiceResult->insertResult(die); } + + /* quint64 threadCount = QThread::idealThreadCount(); + if(threadCount>m_diceCount) + { + threadCount=m_diceCount; + } + quint64 dicePass = m_diceCount/threadCount; + quint64 remainingDiceCount=m_diceCount; + + QThreadPool threadpool; + + + for(int i=threadCount-1;i>=0 && remainingDiceCount>0;--i) + { + remainingDiceCount-=dicePass; + if((remainingDiceCount<dicePass)||((i==0)&&(remainingDiceCount!=0))) + { + dicePass+=remainingDiceCount; + remainingDiceCount=0; + } + qDebug() << remainingDiceCount << dicePass << i << threadCount; + threadpool.start(new DiceRoller(m_mutex,m_myDiceResult,m_faces,dicePass)); + } + + threadpool.waitForDone();*/ if(NULL!=m_nextNode) { m_nextNode->run(this); } } } + + + } quint64 DiceRollerNode::getFaces() |