| 46 | } |
| 47 | |
| 48 | void ThreadPool::WorkerFunction(void* arg) |
| 49 | { |
| 50 | ThreadStruct* threadStruct = static_cast<ThreadStruct*>(arg); |
| 51 | |
| 52 | LOGD("Worker thread {} is starting", Thread::GetCurrentId()); |
| 53 | |
| 54 | while (true) { |
| 55 | threadStruct->queueMutex->Lock(); |
| 56 | while (threadStruct->queue->empty() && !threadStruct->shouldQuit) { |
| 57 | threadStruct->queueCV->Wait(*(threadStruct->queueMutex)); |
| 58 | } |
| 59 | |
| 60 | if (threadStruct->shouldQuit) { |
| 61 | threadStruct->queueMutex->Unlock(); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<IThreadCommand> threadCommand = std::move(threadStruct->queue->front()); |
| 66 | threadStruct->queue->pop_front(); |
| 67 | threadStruct->queueMutex->Unlock(); |
| 68 | |
| 69 | LOGD("Worker thread {} is executing its command", Thread::GetCurrentId()); |
| 70 | threadCommand->Execute(); |
| 71 | } |
| 72 | |
| 73 | LOGD("Worker thread {} is exiting", Thread::GetCurrentId()); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |