| 251 | } |
| 252 | |
| 253 | void WorkQueue::WorkerThreadProc() |
| 254 | { |
| 255 | std::ostringstream idbuf; |
| 256 | idbuf << "WQ #" << m_ID; |
| 257 | Utility::SetThreadName(idbuf.str()); |
| 258 | |
| 259 | l_ThreadWorkQueue.reset(new WorkQueue *(this)); |
| 260 | |
| 261 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 262 | |
| 263 | for (;;) { |
| 264 | while (m_Tasks.empty() && !m_Stopped) |
| 265 | m_CVEmpty.wait(lock); |
| 266 | |
| 267 | if (m_Stopped) |
| 268 | break; |
| 269 | |
| 270 | if (m_Tasks.size() >= m_MaxItems && m_MaxItems != 0) |
| 271 | m_CVFull.notify_all(); |
| 272 | |
| 273 | Task task = m_Tasks.top(); |
| 274 | m_Tasks.pop(); |
| 275 | |
| 276 | m_Processing++; |
| 277 | |
| 278 | lock.unlock(); |
| 279 | |
| 280 | RunTaskFunction(task.Function); |
| 281 | |
| 282 | /* clear the task so whatever other resources it holds are released _before_ we re-acquire the mutex */ |
| 283 | task = Task(); |
| 284 | |
| 285 | IncreaseTaskCount(); |
| 286 | |
| 287 | lock.lock(); |
| 288 | |
| 289 | m_Processing--; |
| 290 | |
| 291 | if (m_Tasks.empty()) |
| 292 | m_CVStarved.notify_all(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void WorkQueue::IncreaseTaskCount() |
| 297 | { |