OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJobMultiThreadQueue.cpp
Go to the documentation of this file.
2 
3 ossimJobMultiThreadQueue::ossimJobMultiThreadQueue(std::shared_ptr<ossimJobQueue> q,
4  ossim_uint32 nThreads)
5 :m_jobQueue(q?q:std::make_shared<ossimJobQueue>())
6 {
7  setNumberOfThreads(nThreads);
8 }
9 
11 {
12  cancel();
14  m_threadQueueList.clear();
15 }
16 
17 std::shared_ptr<ossimJobQueue> ossimJobMultiThreadQueue::getJobQueue()
18 {
19  std::lock_guard<std::mutex> lock(m_mutex);
20  return m_jobQueue;
21 }
22 const std::shared_ptr<ossimJobQueue> ossimJobMultiThreadQueue::getJobQueue()const
23 {
24  std::lock_guard<std::mutex> lock(m_mutex);
25  return m_jobQueue;
26 }
27 void ossimJobMultiThreadQueue::setJobQueue(std::shared_ptr<ossimJobQueue> q)
28 {
29  std::lock_guard<std::mutex> lock(m_mutex);
30  ossim_uint32 idx = 0;
31  m_jobQueue = q;
32  for(idx = 0; idx < m_threadQueueList.size(); ++idx)
33  {
34  m_threadQueueList[idx]->setJobQueue(m_jobQueue);
35  }
36 }
38 {
39  std::lock_guard<std::mutex> lock(m_mutex);
40  ossim_uint32 idx = 0;
41  ossim_uint32 queueSize = m_threadQueueList.size();
42 
43  if(nThreads > queueSize)
44  {
45  for(idx = queueSize; idx < nThreads;++idx)
46  {
47  std::shared_ptr<ossimJobThreadQueue> threadQueue = std::make_shared<ossimJobThreadQueue>();
48  threadQueue->setJobQueue(m_jobQueue);
49  m_threadQueueList.push_back(threadQueue);
50  }
51  }
52  else if(nThreads < queueSize)
53  {
54  ThreadQueueList::iterator iter = m_threadQueueList.begin()+nThreads;
55  while(iter != m_threadQueueList.end())
56  {
57  (*iter)->cancel();
58  iter = m_threadQueueList.erase(iter);
59  }
60  }
61 }
62 
64 {
65  std::lock_guard<std::mutex> lock(m_mutex);
66  return static_cast<ossim_uint32>( m_threadQueueList.size() );
67 }
68 
70 {
71  ossim_uint32 result = 0;
72  std::lock_guard<std::mutex> lock(m_mutex);
73  ossim_uint32 idx = 0;
74  ossim_uint32 queueSize = m_threadQueueList.size();
75  for(idx = 0; idx < queueSize;++idx)
76  {
77  if(m_threadQueueList[idx]->isProcessingJob()) ++result;
78  }
79  return result;
80 }
81 
83 {
84  std::lock_guard<std::mutex> lock(m_mutex);
85  ossim_uint32 idx = 0;
86  ossim_uint32 queueSize = m_threadQueueList.size();
87  for(idx = 0; idx < queueSize;++idx)
88  {
89  if(!m_threadQueueList[idx]->isProcessingJob()) return false;
90  }
91 
92  return true;
93 }
94 
96 {
97  bool result = false;
98  {
99  std::lock_guard<std::mutex> lock(m_mutex);
100  ossim_uint32 queueSize = m_threadQueueList.size();
101  ossim_uint32 idx = 0;
102  for(idx = 0; ((idx<queueSize)&&!result);++idx)
103  {
104  result = m_threadQueueList[idx]->hasJobsToProcess();
105  }
106  }
107 
108  return result;
109 }
110 
112 {
113  for(auto thread:m_threadQueueList)
114  {
115  thread->cancel();
116  }
117 }
118 
120 {
121  for(auto thread:m_threadQueueList)
122  {
123  thread->waitForCompletion();
124  }
125 }
126 
ossim_uint32 getNumberOfThreads() const
std::shared_ptr< ossimJobQueue > getJobQueue()
void cancel()
Allows one to cancel all threads.
void setJobQueue(std::shared_ptr< ossimJobQueue > q)
set the job queue to all threads
void waitForCompletion()
Allows on to wait for all thread completions.
unsigned int ossim_uint32
void setNumberOfThreads(ossim_uint32 nThreads)
Will set the number of threads.
ossimJobMultiThreadQueue(std::shared_ptr< ossimJobQueue > q=0, ossim_uint32 nThreads=0)
allows one to create a pool of threads with a shared job queue
This is the base implementation for the job queue.
Definition: ossimJobQueue.h:84
virtual ~ossimJobMultiThreadQueue()
Will cancel all threads and wait for completion and clear the thread queue list.
ossim_uint32 numberOfBusyThreads() const
std::shared_ptr< ossimJobQueue > m_jobQueue