I'm writing a Windows service in C, using the classic example found here: https://learn.microsoft.com/en-us/windows/desktop/Services/svc-cpp. I'm trying to understand why CPU usage of my service is 25% when it is not doing anything.
I have taken out just about all of my code, leaving only the original skeleton program. Basically it is in a tight loop where it checks for the service stop command, then a Sleep(0), then loops back. In my real code, I am listening for a tcp connection, but that is out of the equation in this test.
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv)
{
// initialization code not shown here
// Main loop
while(TRUE) {
// Check whether to stop the service.
ret=WaitForSingleObject(ghSvcStopEvent, 0);
if (ret == 0) {
com_Log("Stop command received from service manager");
break;
}
Sleep(0);
// This is where the service looks for work to do, but disabled for
testing
}
// shutdown code here
}
I thought the Sleep(0) is what returns control to the operating system, so why would this code be misbehaving and taking up such a big chunk of CPU?