ThreadManager.cpp
Go to the documentation of this file.
1 #include "../../../common/PlatformUtils.h"
2 #include "DrawThread.h"
3 #include "ThreadManager.h"
4 
5 // Qt
6 #include <QThreadPool>
7 #include <QTimer>
8 
10  const std::vector<QRunnable*>& threads, int interval)
11  :
12 
13  m_threads(threads),
14  m_pool(nullptr),
15  m_interval(interval)
16 {
17  m_timer = new QTimer(this);
18  connect(m_timer, SIGNAL(timeout()), SIGNAL(showFeedback()));
19 }
20 
22 
24 {
25  m_timer->start(m_interval);
26 
28  {
29  m_pool = nullptr;
30 
31  for (std::vector<QRunnable*>::iterator it = m_threads.begin(); it != m_threads.end(); ++it)
32  {
33  ((DrawThread*)*it)->run();
34  }
35 
36  m_timer->stop();
37 
38  emit finished();
39  }
40  else
41  {
42  m_pool = new QThreadPool;
43 
44  for (std::vector<QRunnable*>::iterator it = m_threads.begin(); it != m_threads.end(); ++it)
45  {
46  connect((DrawThread*)*it, SIGNAL(finished()), SLOT(onThreadFinished()));
47  m_pool->start(*it);
48  }
49  }
50 }
51 
53 {
54  if(m_pool == nullptr)
55  return;
56 
57  for(std::vector<QRunnable*>::iterator it = m_threads.begin(); it != m_threads.end(); ++it)
58  ((DrawThread*)*it)->m_cancel = true;
59 
60  m_pool->waitForDone();
62 }
63 
65 {
66  if(m_pool == nullptr)
67  return;
68 
69  if(m_pool->activeThreadCount() == 0)
70  {
71  m_timer->stop();
72 
73  delete m_pool;
74  m_pool = nullptr;
75 
76  emit finished();
77  }
78 }
Thread to draw a Layer in a PaintDevice.
std::vector< QRunnable * > m_threads
Definition: ThreadManager.h:74
Manager for threads of rendering.
TECOMMONEXPORT unsigned int GetPhysProcNumber()
Returns the number of physical processors.
ThreadManager(const std::vector< QRunnable * > &threads, int interval=-1)