Skip to content

Instantly share code, notes, and snippets.

@dereckson
Last active August 29, 2015 14:21
Show Gist options
  • Save dereckson/0d2edd698ba14353bf1a to your computer and use it in GitHub Desktop.
Save dereckson/0d2edd698ba14353bf1a to your computer and use it in GitHub Desktop.
Use of usleep to idle a thread and avoid full CPU core use.
#include <stdio.h>
#ifdef IDLE
#include <unistd.h>
#endif
int main () {
int a = 0, b = 0, sum = 0;
while (1) {
sum = a + b;
#ifdef IDLE
usleep(1000);
#endif
}
}
all: idle-on idle-off
idle-on:
${CC} -DIDLE idle_test.c -o idle-on
idle-off:
${CC} idle_test.c -o idle-off
clean:
rm -f *.o idle-on idle-off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment