Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Threading
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 18-Apr-13 22:08pm

1 solution

Rate this: bad
good
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_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";
    }
    
}
  Permalink  
Comments
Aayman Khalid at 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?
du[DE] at 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)

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100