Currently, ThreadPool's destructor does not wait for completion of all tasks posted to it, because Worker relies on m_running_flag to stay in the loop, as a result this flag is set to false as soon as ThreadPool destructor is invoked, as the threads exit without further processing the tasks from the queue.
One solution is to remove m_running_flag from the worker, and instead add a poison task to the queue and each worker reads this poison task and exit but before exiting enqueue it again, so that other workers can exit in similar mannner.
Also, ThreadPool may provide a function called wait() function which will add the poison task. Also, the destructor should call this wait() function.
Currently, ThreadPool's destructor does not wait for completion of all tasks posted to it, because
Workerrelies onm_running_flagto stay in the loop, as a result this flag is set tofalseas soon asThreadPooldestructor is invoked, as the threads exit without further processing the tasks from the queue.One solution is to remove
m_running_flagfrom the worker, and instead add a poison task to the queue and each worker reads this poison task and exit but before exiting enqueue it again, so that other workers can exit in similar mannner.Also,
ThreadPoolmay provide a function calledwait()function which will add the poison task. Also, the destructor should call thiswait()function.