samedi 27 juin 2020

Can we avoid using Sleep() API to ensure that threads are not blocked unnecessarily in C++?

In the below function, I am assuming that we can avoid using Sleep() API to ensure that threads are not blocked unnecessarily.

Below is the code snippet and explanation:

void CApplicationListener::createApplicationLaunchThread()
{
    CReqXMLMessageObject *l_objMesgObject = nullptr;

    while (!gShutDown)
    {
        if (m_hConnectStatus == SOCKET_ERROR)
        {
            bool l_boIsApplicationRun = CAppUtility::launchApplicationProcess(m_hApplicationProcess);
            // Here l_boIsApplicationRun returns true
            if (l_boIsApplicationRun)
            {
                if (startApplicationListener())
                {
                    if (m_hConnectStatus != SOCKET_ERROR)
                    {
                        Sleep(1200);
                        l_objMesgObject = new CReqXMLMessageObject();
                        l_objMesgObject->sendReqSFLoggedInXML();
                        if (l_objMesgObject != nullptr)
                        {
                            delete l_objMesgObject;
                            l_objMesgObject = nullptr;
                        }
                    }
                }
            }
        }
        
        Sleep(100);
    }
}

Information about the above function:

In the above function, we are invoking launchApplicationProcess. This function is checking the application process is running or not. If it is not launching, it is creating and starting the application process.

In our case it is launching the process and "l_boIsApplicationRun" returns true.

Then calling startApplicationListener function. This function reads the port no form the configuration file, creating the socket and initializing the socket connection and then connecting to the port no.

But in the above function and also in the project, I wanted to know the options to avoid using Sleep() API and ensure that threads are not blocked unnecessarily. And also if it is required to use the sleep to pause the execution temporarily, I was asked to use the platform independent function instead of using Sleep() API.

I Wanted to know the experts inputs/suggestions to use or remove the Sleep() function.

What are the consequnces if we remove the Sleep() function?

Aucun commentaire:

Enregistrer un commentaire