summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-06 00:43:24 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-06 00:43:24 -0800
commit0e1eed88f1adf24923d03c120186f131dbc020fd (patch)
tree511bd00ff0ec202979881ab223bde9475dbf7cf1
parent163bd4955c3b86fa2fa5f89b010412f2e71260eb (diff)
fixed a bug where the listener thread did not exit when ctrl+c was sent. updated license
-rw-r--r--LICENSE2
-rw-r--r--snake.c16
2 files changed, 11 insertions, 7 deletions
diff --git a/LICENSE b/LICENSE
index fb57b38..553f98c 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright 2022 Kai Stevenson
+Copyright 2023 Kai Stevenson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
diff --git a/snake.c b/snake.c
index dc64d0a..f7097d9 100644
--- a/snake.c
+++ b/snake.c
@@ -2,15 +2,14 @@
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
-#include <signal.h> /* handling ctrl-c */
+#include <signal.h> //handling ctrl-c
#include "vector.h"
#include "snake.h"
#include "conio.h"
volatile sig_atomic_t INT_CTRL_C = 0;
-/* Unsafe functions like printf cannot be called in async
- * interrupt handler */
+// unsafe functions like printf cannot be called in async interrupt handler
void interrupt_handler(int sig) {
INT_CTRL_C = 1;
}
@@ -24,7 +23,7 @@ vector_t lastMoveDir;
int main(int argc, char **argv) {
printf("\e[?25l"); /* hide cursor */
- /* Make a new thread handling interrupts */
+ //make a new thread handling interrupts
signal(SIGINT, interrupt_handler);
//init values
@@ -51,10 +50,15 @@ int main(int argc, char **argv) {
pthread_create(&listenerID, NULL, get_dir_loop, &threadArg);
//main game logic loop
while(1) {
- /* handle ctrl-c interrupt */
+ // handle ctrl-c interrupt
if (INT_CTRL_C) {
+ //unhide cursor
printf("\e[?25h");
- exit(0);
+ //kill the listener thread
+ *cancellationToken = 1;
+ printf("Press any key to exit\n");
+ pthread_join(listenerID, NULL);
+ return 0;
}
//first, draw the current state of the board