Parallel Programming - Finished(?) pthread version
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / main.c
index 2d6b228..5316246 100644 (file)
@@ -10,10 +10,15 @@ unsigned numberOfProcessors;
 
 System universe;
 pthread_t compute_thread;
-pthread_mutex_t mutex_terminate;
-bool terminate = false;
+pthread_mutex_t mutex_runstate;
+RUNSTATE runstate = RUN;
 
-void Cleanup_Threads(void); //Cleanup for threads
+void Thread_Cleanup(void)
+{
+       if (runstate == RUN)
+               QuitProgram(false);
+       pthread_join(compute_thread, NULL);
+}
 
 // This is main function. Do not change it.
 int main(int argc, char** argv)
@@ -33,7 +38,7 @@ int main(int argc, char** argv)
                numberOfProcessors = atoi(argv[2]);
        }
        
-       atexit(Cleanup_Threads);
+       atexit(Thread_Cleanup);
        
        // Create a thread to handle computation loop
        
@@ -45,20 +50,10 @@ int main(int argc, char** argv)
        
        Graphics_Run(argc, argv); // Run graphics loop in the main thread
        
+       printf("Close!\n");
+       //We get to this point when the "close" button is clicked in the graphics window
+       QuitProgram(false);
+       pthread_join(compute_thread, NULL);
        
 }
 
-/**
- * Function will be called when exit() is called.
- * Will set condition for child threads to terminate, and then join with them.
- * The main thread is responsible for calling exit().
- */ 
-void Cleanup_Threads()
-{
-       printf("Set terminate\n");
-       pthread_mutex_lock(&mutex_terminate);
-       terminate = true;
-       pthread_mutex_unlock(&mutex_terminate);
-       pthread_join(compute_thread, NULL);
-       pthread_exit(NULL);
-}

UCC git Repository :: git.ucc.asn.au