Search code examples
cposix

Is usleep() in C implemented as busy wait?


I'm building a multithreaded application with pthreads and need a thread to periodically check some stuff. During the time in between this thread shouldn't use any CPU. Is this possible with usleep()? Is usleep() not busy waiting? Or is there a better solution?


Solution

  • The function usleep has been removed from SUSv4. You should probably use nanosleep instead or timers (setitimer, etc).

    As R.. notes in the comments, should the sleep be implemented as a busy wait:

    • The thread would continue to use the CPU
    • Other (lower-priority) threads wouldn't get a chance to run

    Thus:

    • Some might use signals (I think SUSv3 mentioned SIGALARM?)
    • Some might use fancy timers