From 207f22bc4edd913e9e8c279eeba10caafa8df16b Mon Sep 17 00:00:00 2001 From: Renaud G Date: Mon, 17 Nov 2014 07:08:49 +0100 Subject: Add multithreading support but it seems slower. It has been disable. --- node/dicerollernode.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'node/dicerollernode.cpp') 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 +#include #include +#include + + +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((remainingDiceCountrun(this); } } } + + + } quint64 DiceRollerNode::getFaces() -- cgit v1.2.3-70-g09d2