Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I obtain Sleep(0) like behaviour in Linux

Tags:

c

linux

On Windows Sleep(0) yields thread control without specifying a minimum time, (see here) But on Linux and POSIX sleep(0) from unistd.h is just ignored.

What's the best way to get Sleep(0) like behaviour in Linux?

like image 498
Chris Huang-Leaver Avatar asked Jun 28 '11 09:06

Chris Huang-Leaver


1 Answers

Try sched_yield from "sched.h", that is created just for what you want to do.

If that doesn't work for whatever reason - try usleep(1) - That will yield at least 1 microsecond delay (may be more), which in turn triggers a context switch (if any thread is waiting).

like image 67
littleadv Avatar answered Oct 13 '22 00:10

littleadv