X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fmthread%2Fmain.c;fp=course%2Fsemester2%2Fpprog%2Fassignment1%2Fmthread%2Fmain.c;h=531624616e1c2a271d827fc3e47fe60fedc8e742;hb=19cb61f75ddda901224f7ce2bae0d3934f67d9ee;hp=2d6b228b422e0018b9f05662f707c75bdd95e0ad;hpb=7a341db1fbf5db94b711135b2a79e852c6fb1bc4;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/mthread/main.c b/course/semester2/pprog/assignment1/mthread/main.c index 2d6b228b..53162461 100644 --- a/course/semester2/pprog/assignment1/mthread/main.c +++ b/course/semester2/pprog/assignment1/mthread/main.c @@ -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); -}