Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey,
I am new to the world of thread pools and I've been searching a lot about it but get confused in the mean process(due to a lot of advanced stuff)...
at first I just want to get the hang of creating a thread pool and destroying it...
could someone lay out the basic steps needed to do this and the relevant functions in c/c++ for me?
I am asking this question after having searched a lot myself...
Posted

1 solution

Take a look: Simplest ThreadPool example Using C++[^]

What is ThreadPool?
Thread Pool is collection of Live, Reusable threads.

Why ThreadPool?
There are many situation where we can use ThreadPool. Consider a Client-Server application in which server has to respond to multiple client at same time. This means multitasking. Server will need a set of certain no. of threads which will do reply to client.
The big advantage of thread pool is that, it provides reusable thread. Though thread creation is very bulky process. It increases overhead. Suppose a new client send some data to server and Server has to respond. What will server do? It will create a new thread which will send data to client and then get killed or exited. Instead of creating thread on each client request we can keep collection of live thread. We will use any free thread which will send data to client. By that we will save a great overhead generated by creating threads multiple times. ...

C++
/****************************************************************************
CThreadPoolMgr Initialize 
Description : Creates threads. Thread limit is 5
****************************************************************************/
void CThreadPoolMgr::Initialize(int nThread)
{
    m_nThreadCount = nThread;

    int nCounter = 0;
    int nThreadCount = m_nThreadCount - 1;
    
    while( nCounter <= nThreadCount )
    {
        // Create objects in heap
        m_ptrCThread[nCounter] = new CThread();

        m_ptrCThread[nCounter]->CreateWorkerThread();
        m_hThreadPool[nCounter] = m_ptrCThread[nCounter]->GetThreadHandle();
        // Increment the counter
        nCounter++;
    }    
}



/****************************************************************************
CThreadPoolMgr ShutDown 
Description : Mark shutdown siganl and wait for each thread to end 
****************************************************************************/
void CThreadPoolMgr::ShutDown()
{
    int Count = 0;

    while(Count <= (m_nThreadCount - 1))
    {
        m_ptrCThread[Count]->SignalShutDownEvent();
        
        Count++;
    }

    // Check if all threads ended successfully
    DWORD dwWaitResult = WaitForMultipleObjects( GetThreadCount(), m_hThreadPool, TRUE, INFINITE);
    
    switch(dwWaitResult)
    {
    case WAIT_OBJECT_0:
        {
            cout << "All threads are ended.\n";
            // Close all handles 
            Count = 0;
            while( Count <= (m_nThreadCount - 1))
            {
                m_ptrCThread[Count]->ReleaseHandles();
                delete m_ptrCThread[Count];
                Count++;
            }
            
            break;
        }

    default:
        cout << "Wait Error: " << GetLastError() << "\n";
    }
    
}
 
Share this answer
 
Comments
Aayman Khalid 19-Apr-13 3:22am    
Thank you so much for such an elaborate answer:)
just one more thing,do I need to include some specific header file for this?
Leo Chapiro 19-Apr-13 3:33am    
No, as far as I see you don't need any specific headers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900