Creating thread pools in C ++


I am not sure how to put this question in this forum any way i am asking and hopefully get some inputs.

I am writing a thread pool for my project. I have following design.

  1. I am maintaining vector of threads std::vector<ThreadWrapper <threadFuncParam>* > m_vecThreads;

  2. and pushing the threds in to list m_vecThreads.push_back(pThreadWrapper);

  3. When new request comes i am taking the thread pool as below

    if(!m_vecThreads.empty() )
        ThreadWrapper <threadFuncParam>* pWrapper = m_vecThreads.back();
        //... Awake threadd
  4. When thread job is done it is pushed back in to pool of thread.

Now while gracefull shutdown i have stop the threads gracefully now with the design above i am facing problem how can i stop threads as in vector container i am poping from vector when request is serviced, so i lost the pointer till service is completed. Is there better i can do this or handle this scenario like map or other container which is supported by standard C++?

Another question is

During shutdown i have a scenario threads are doing process here in my case reading from database which may take time so i cannot wait till it is complete and i want to send reply to clients for pending requests which threads are processing and i am about to kill that value is bad.


If you still need access to what you pass out from your pool, then you should store the items in a "used" container.
However, at that moment, you are sharing your pointers, so you should use shared_ptr and pass out weak_ptr, so the threads can also be deleted and the users don't have a dangling pointer

The best cointainer for the used items would be a set, so the returned thread can be found and removed easily.