Class CThreadPool::
PoolThread()
Base ClassesData ItemsConstructorsDestructorsFunctionsCustom CodeGo to hierarchy chart    Prev page: operator =Next page: ReSetThreadCount    Show member index
Private Function Declared in:
ThreadPool.h

'Declaration' icon -- Shortcut to top of page. Declaration

static DWORD PoolThread(
    LPVOID pParam);

'Description' icon -- Shortcut to top of page. Description

Our thread proc

'Function Body' icon -- Shortcut to top of page. Function Body

try
{
    //This is the thread class
    ThreadData* pData;
    pData=(ThreadData*)pParam;

    //Initialize the random seed
    srand(GetTickCount()+((unsigned long)pData->pEvent)*2);

    //Error for initialize proc
    BOOL bError;
    bError=FALSE;

    //Our data
    LPVOID lpData;
    lpData=NULL;

    //Call the initialize proc (if exists)
    if (pData->pInitializeProc)
    {
        try
        {
            bError=!(*pData->pInitializeProc)(lpData,TRUE);
        }
        catch (...)
        {
            bError=TRUE;
        }
    }

    //Set the event
    if (pData->pEvent)
        if (!bError)
            pData->pEvent->Set();
        else
        {
            //Report it
            ReportStaticError(CThreadPool_Class,"PoolThread","Initialize proc failed!");

            //Exit
            return FALSE;
        }
    else
    {
        //Report it
        ReportStaticError(CThreadPool_Class,"PoolThread","Received null event!");

        //Exit
        return FALSE;
    }

    //Our wait list
    CWaitList aList;
    aList.AddObject(pData->pClass->GetSemaphore(),TRUE);
    aList.AddObject(pData->pExitEvent);

    //Endless loop
    while (1)
    {
        DWORD dwObject;

        //Wait for the events
        aList.Wait(FALSE,dwObject);

        //What do we have
        if (dwObject)
            break;

        //Increase the count
        pData->pClass->ReSetThreadCount(1);

        //Take a job
        JobData aJobData;
        pData->pClass->GetJob(aJobData);

        //Need to protect running thread
        try
        {
            //Check it's a valid proc
            if (aJobData.pProc)
                //Run it
                (*aJobData.pProc)(aJobData.pParam);
            else if (aJobData.pDWORDProc)
                //Run it
                (*aJobData.pDWORDProc)(aJobData.pParam);
            else if (aJobData.pDataProc)
                (*aJobData.pDataProc)(aJobData.pParam,lpData);
        }
        ERROR_HANDLER_STATIC(CThreadPool_Class,"PoolThread - Proc")

        //Release the count
        pData->pClass->ReSetThreadCount(-1);
    }

    //Call the unload
    if (pData->pInitializeProc)
    {
        try
        {
            bError=!(*pData->pInitializeProc)(lpData,FALSE);
        }
        catch (...)
        {
            bError=TRUE;
        }
    }

    //We are OK to exit
    pData->pEvent->Set();

    //Done
    return TRUE;
}
ERROR_HANDLER_STATIC_RETURN(CThreadPool_Class,"PoolThread",FALSE)

'See Also' icon -- Shortcut to top of page. See Also

Class Overview Class Overview  |  Public base class CErrorHandler  |  Hierarchy Chart Hierarchy Chart


Get Surveyor!This web site was generated using Surveyor V4.50.811.1.  Click here for more information. Site content copyright © 2003 Komodia LTD.. See the About page for additional notices. This page last updated: 24 Feb 2003.