Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is usleep() in C implemented as busy wait?

Tags:

c

posix

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?

like image 580
zeebonk Avatar asked Nov 16 '11 18:11

zeebonk


1 Answers

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
like image 96
cnicutar Avatar answered Nov 04 '22 23:11

cnicutar