From c8a444f1416c62879ad1d3ffb97019c25d24d79d Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sat, 15 May 2021 22:34:30 -0400 Subject: [PATCH] Sleep during loops Doesn't max out the CPU --- pong.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pong.c b/pong.c index 0a75050..c158607 100644 --- a/pong.c +++ b/pong.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,14 @@ randsign(void) return pow(-1, rand()); } +void +msleep(void) +{ + struct timeval timeout; + timeout.tv_usec = 10000; + select(NULL, NULL, NULL, NULL, &timeout); // Sleep 100 ms +} + int main(void) { srand((unsigned int)time(NULL)); @@ -95,6 +104,7 @@ main(void) { } else if (key == 'q') { die(0); } + msleep(); } } @@ -177,5 +187,6 @@ main(void) { mvprintw(y, x, " "); y = (y == max_y) ? max_y - 1 : 1; } + msleep(); } } -- 2.39.2