Home > Software engineering >  How to make the thread is idle CPU resources
How to make the thread is idle CPU resources

Time:09-20

10 requirement is: I have the same thread (the function content is consistent, only trigger point) to the main line synchronization, in the absence of the task, hope thread being idle tenses, CPU resources, but can't SLEEP, has arrived, the main thread task thread to immediate execution, the
Of course I know that in the general case, let thread to Sleep when idle, but the thread to Sleep later, what time to take control of the CPU is determined by the operating system, when the task arrives, the thread does not wake up immediately, so that even Sleep (1) milliseconds can't meet the requirements,

CodePudding user response:

The WaitForSingleObject wait Event

CodePudding user response:

1 you can't delay, ms means you need to keep monitoring the tasks to be reached, means that you need CPU resources all the time,,,,

Infinite loop will always CPU, generally, the sleep logic is:

While (1)
{
If (NoTask)
Sleep (n);
The else
DoTask ();
}

CodePudding user response:


(WaitForSingleObject wait Event) I have ten conditions how to set up wait for events is ruled in the for loop?

CodePudding user response:

Outside the for events such as not?

While (1)
{
HANDLE hEventArray []={hExitEvent, hDoEvent};
DWORD dwWait=WaitForMultipleObjects (2, hEventArray, FALSE, INFINITE).
The switch (dwWait)
{
Case (WAIT_OBJECT_0 + 0) ://exit event
{
return 1;
}
Case (WAIT_OBJECT_0 + 1) ://do event
{
//add you code here
break;
}
Default://unexcept
{
ASSERT (0);
Retrun - 1;
}
}
}

CodePudding user response:

If waiting for more than one event use WaitForMultipleObjects
Note WaitForMultipleObjects return not only when an event is in the signaled state,

CodePudding user response:

Sleep...

CodePudding user response:

If waiting for more than one event use WaitForMultipleObjects
Note WaitForMultipleObjects return not only when an event is in the signaled state,


More than one event is in the signaled state how to deal with the great spirit are urgent

CodePudding user response:

WaitForMultipleObjects usage was clear to have a look at the help documentation,
DWORD WaitForMultipleObjects (
DWORD nCount,//time number
Const lpHandles HANDLE * and//events list
BOOL bWaitAll,//all events meet to return, or an event to meet return
DWORD dwMilliseconds//timeout
);

Return value: the first event meet, return WAIT_OBJECT_0, second + 1 is good and so on

CodePudding user response:

  • Related