Last active
August 29, 2015 14:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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