Click here to Skip to main content
14,239,046 members
Rate this:
Please Sign up or sign in to vote.
See more:
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...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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. ...

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_hThreadPool[nCounter] = m_ptrCThread[nCounter]->GetThreadHandle();
        // Increment the counter

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

    while(Count <= (m_nThreadCount - 1))

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

        cout << "Wait Error: " << GetLastError() << "\n";
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?
_duDE 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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100