From 762ce9fa6a58d2e00174ad55b5969cabb7ff0dd6 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Wed, 13 Nov 2019 03:31:33 -0500
Subject: [PATCH] Add optional loop option for "safe" input

---
 C/main.c                  | 22 +++++++++++++++++++---
 documents/c_references.md |  5 +++--
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/C/main.c b/C/main.c
index 4e2abc6..8e2ffad 100644
--- a/C/main.c
+++ b/C/main.c
@@ -38,7 +38,7 @@ int shared_counter; // Shared counter for threads to modify. Used to test that l
  */
 int main(int argc, char* argv[]) {
     // Declare variables.
-    char* user_input = calloc(2, sizeof(char));
+    char* user_input = calloc(50, sizeof(char));
 
     printf("Initializing program.\n");
 
@@ -46,13 +46,27 @@ int main(int argc, char* argv[]) {
     printf("Choose an option:\n");
     printf("   1) Unsafe Threading.\n");
     printf("   2) Safe Threading.\n");
-    fgets(user_input, 2, stdin);
+    fgets(user_input, 50, stdin);
 
     // Check user input.
     if (user_input[0] == '1') {
         run_unsafe_threads();
     } else if (user_input[0] == '2') {
-        run_safe_threads();
+        printf("Run safe threading on loop?\n");
+        free(user_input);
+        user_input = calloc(50, sizeof(char));
+        fgets(user_input, 50, stdin);
+
+        if (user_input[0] == 'Y' || user_input[0] == 'y') {
+            // Run on loop until user cancels.
+            printf("\n\nProgram will loop until cancelled with ctrl+c.\n\n");
+            while (1) {
+                run_safe_threads();
+            }
+        } else{
+            run_safe_threads();
+        }
+
     } else {
         printf("Unknown input \"%s\".\n", user_input);
     }
@@ -77,6 +91,7 @@ void run_unsafe_threads() {
     int thread_b_result;
 
     printf("Starting threads...\n\n");
+    shared_counter = 0;
 
     // Create threads and threading values.
     thread_pool = calloc(2, sizeof(pthread_t));
@@ -108,6 +123,7 @@ void run_safe_threads() {
     int thread_b_result;
 
     printf("Starting threads...\n\n");
+    shared_counter = 0;
 
     // Create threads and threading values.
     thread_pool = calloc(2, sizeof(pthread_t));
diff --git a/documents/c_references.md b/documents/c_references.md
index 5d5cce4..8e32623 100644
--- a/documents/c_references.md
+++ b/documents/c_references.md
@@ -22,5 +22,6 @@ All references to external logic. Includes anything from stack overflow links to
 * <https://stackoverflow.com/questions/17846212/generate-a-random-number-between-1-and-10-in-c>
 
 ### Getting User Input in C
-* <https://stackoverflow.com/questions/22065675/get-text-from-user-input-using-c>
-* <https://stackoverflow.com/a/10575525>
+* General Input Info - <https://stackoverflow.com/questions/22065675/get-text-from-user-input-using-c>
+* Info on fgets - <https://stackoverflow.com/a/10575525>
+* Fgets seeming inconsistent - <https://stackoverflow.com/a/38768067>
-- 
GitLab