Click here to Skip to main content
Licence 
First Posted 24 Aug 2002
Views 43,833
Bookmarked 10 times

Thread pool class Bug Fixed

By | 24 Aug 2002 | Article
Fixed a bug about Thread pool class submited by Sherwood Hu

Thread pool

Last week I find those cool code shared by Sherwood Hu. You can view the article named "A programming model to use a thread pool" for details of this class. I used this class to boot the performance of my application. But a strange phenomenon puzzled me for a long time. That is: it works perfectly when I run my application normally, but suspends sometimes when I debug my application. I located the bug finally after some "Ctrl+Alt+Del". Check the original code here:

unsigned int CThreadPool::ManagerProc(void* p)
{
    //convert the parameter to the server pointer.
    CThreadPool* pServer=(CThreadPool*)p;
    HANDLE    IoPort    = pServer->GetMgrIoPort();
    unsigned long pN1, pN2; 
    OVERLAPPED* pOverLapped;

LABEL_MANAGER_PROCESSING:
    while(::GetQueuedCompletionStatus(IoPort, &pN1, &pN2, 
        &pOverLapped, pServer->GetMgrWaitTime() ))
    {
        if(pOverLapped == (OVERLAPPED*)0xFFFFFFFF)
            break;
        else
        {
            TRACE0("mgr events comes in!\n");
        }
    }

    //time out processing
    if (::GetLastError()==WAIT_TIMEOUT)
    {
        //time out processing
        TRACE0("Time out processing!\n");
        //the manager will take a look at all the worker's status. The
        if (pServer->GetThreadPoolStatus()==CThreadPool::BUSY)
            pServer->AddThreads();
        if (pServer->GetThreadPoolStatus()==CThreadPool::IDLE)
            pServer->RemoveThreads();

        goto LABEL_MANAGER_PROCESSING;
    }
    return 0;
}

The CThreadPool::Stop function uses:

::PostQueuedCompletionStatus(m_hMgrIoPort, 0, 0, (OVERLAPPED*)0xFFFFFFFF);

to stop the manager thread. The manager thread uses:

::GetQueuedCompletionStatus(IoPort, &pN1, &pN2, 
         &pOverLapped, pServer->GetMgrWaitTime())

to wait this message. When it meets the condition:

pOverLapped == (OVERLAPPED*)0xFFFFFFFF)

the thread stops normal. While this API wait times out, it uses "goto" continue waiting. Quit message and timeout event may not occur if the program runs normally, but it may occur when you debug your code. The manager thread will receive the quit message and timeout event at the same time while you insert the break point in your code and trace it step by step. The manager thread receives the quit message and breaks from while circulation, but is caught by the following if statement and throw to begin by "goto" statement. Since it will never receive quit message again, it will circulate endless. So the application will suspend at here:

void CThreadPool::Stop(bool bHash)
{
......
    WaitForSingleObject(m_hMgrThread, INFINITE);//program will suspend here
......
}

Solution

I add a sign to distinguish the quit message and timeout event, and thus the bug is fixed.

.....
    BOOL bExit = FALSE;
LABEL_MANAGER_PROCESSING:
    while(::GetQueuedCompletionStatus(IoPort, &pN1, &pN2, 
        &pOverLapped, pServer->GetMgrWaitTime() ))
    {
        if(pOverLapped == (OVERLAPPED*)0xFFFFFFFF)
        {
            bExit = TRUE; 
            break;
        }
        else
        {
            TRACE0("mgr events comes in!\n");
        }
    }

    //time out processing
    if (::GetLastError()==WAIT_TIMEOUT && !bExit)
    {
......

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Simon.W

Software Developer (Senior)

China China

Member

I had graduated from HuaZhong University of Science & Technology. Now, I'm working in ZTE.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThread pool fix PinmemberMike Junkin8:10 26 Aug '02  
GeneralRe: Thread pool fix PinmemberXSimon18:18 26 Aug '02  
GeneralRe: Thread pool fix PinmemberAnthony_Yio23:42 5 Jan '03  
GeneralRe: Thread pool fix PinsussMushica14:26 4 Jun '03  
GeneralRe: Thread pool fix PinmemberXSimon1:26 6 Jun '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 25 Aug 2002
Article Copyright 2002 by Simon.W
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid