Is it possible in C/C++, to “abandon” thread’s CPU time?
For example:
void wait(int s) //Low cpu usage
{
int tmp = time();
while(tmp + CLOCKS_PER_SEC * s > time())
__AbandonCPUTime();
//Tell cpu to do sth else.
}
I’ve tried Sleep(0), but it doesn’t work.
Look at C++11’s:
Your CPU may support the
_mm_pause()intrinsic (__asm__("pause")for gcc) which may or may not help you.Otherwise you’ll be looking at OS specific functions:
sched_yield()for pthreads on Linux, orSwitchToThread()on Windows