Class CSocketThreadManager::
SpawnThreads()
Base ClassesData ItemsConstructorsDestructorsFunctionsCustom CodeGo to hierarchy chart    Prev page: SocketThreadNext page: Uninitialize    Show member index
Private Function Declared in:
SocketThreadManager.h

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

BOOL SpawnThreads(
    LPCSTR lpClassName);

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

Spawn the threads

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

try
{
    //Add to this class our thread ID, and a random number
    if (!lpClassName)
    {
        //Calculate our string
        int iStringLength;
        iStringLength=strlen(CSocketThreadManager_Class);

        //Add some size to it
        iStringLength+=50;

        //Allocate the string
        std::auto_ptr<char> pString(new char[iStringLength]);

        //Get our thread ID
        DWORD dwThreadID;
        dwThreadID=GetCurrentThreadId();

        //Get the tick of the system
        DWORD dwProcessID;
        dwProcessID=GetTickCount();

        //Create the string
        sprintf(pString.get(),"%s_%lu_%lu",CSocketThreadManager_Class,
                                           GetCurrentThreadId(),
                                           GetTickCount());

        //And register the window
        //Check if we need to register the class
        if (!m_bRegisteredWindow)
            if (!RegisterClass(pString.get()))
            {
                //Report it
                ReportError("SpawnThreads","Failed to register window!");

                //And quit
                return FALSE;
            }

        //Save the class name
        m_sClassName=pString.get();
    }
    else
        m_sClassName=lpClassName;

    //Start creating threads
    //Allocate the thread structure
    m_pThreadData=new ThreadData[m_ulThreadCount];

    //Initialize the data
    for (int iCount=0;iCount<m_ulThreadCount;++iCount)
    {
        m_pThreadData[iCount].bFreeze=FALSE;
        m_pThreadData[iCount].pEvent=NULL;
        m_pThreadData[iCount].hInstance=NULL;
        m_pThreadData[iCount].hWindowHandle=NULL;
        m_pThreadData[iCount].iSocketCount=0;
        m_pThreadData[iCount].iTimeoutCount=0;
        m_pThreadData[iCount].pThread=NULL;
        m_pThreadData[iCount].pCSection=COSManager::CreateCriticalSection();
    }

    //Our waiting list
    CWaitList aList;

    //Start spawning
    for (int iCounter=0;iCounter<m_ulThreadCount;++iCounter)
    {
        //Copy the class name
        m_pThreadData[iCounter].sClassName=m_sClassName;

        //Create an event
        m_pThreadData[iCounter].pEvent=COSManager::CreateEvent();

        //Add it to the list
        aList.AddObject(m_pThreadData[iCounter].pEvent,TRUE);

        //Set our instance
        m_pThreadData[iCounter].hInstance=m_hInstance;

        //And create it
        m_pThreadData[iCounter].pThread=COSManager::CreateThread(SocketThread);

        //Check the thread has been created
        if (!m_pThreadData[iCounter].pThread->GetThreadID())
        {
            //Report the error
            ReportError("SpawnThreads","Failed to create thread!");

            //Delete the handle array
            CleanThreads(TRUE);

            //Quit
            return FALSE;
        }
        else
            //Start the thread
            m_pThreadData[iCounter].pThread->Start((LPVOID)&m_pThreadData[iCounter]);
    }

    //Tmp position
    DWORD dwTmp;

    if (aList.Wait(TRUE,
                   dwTmp,
                   THREADS_TIMEOUT))
    {
        //Report the error
        ReportError("SpawnThreads","Timeout waiting for threads!");

        //Close the threads
        CleanThreads(TRUE);

        //Exit
        return FALSE;
    }
    else
    {
        //We are OK
        m_bInitialized=TRUE;

        //Done
        return TRUE;
    }
}
ERROR_HANDLER_RETURN("SpawnThreads",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.