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

  • Thread starter Thread starter John paul coder
  • Start date Start date
J

John paul coder

Guest
Hello All,

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:

static bool gShutDown = false;
int m_hConnectStatus;

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);
}
}

Explanation for 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.

And also in startApplicationListener(), we are writing the log then incrementing the int count and using Sleep().

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

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

Continue reading...
 
Back
Top