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=2d6b228b422e0018b9f05662f707c75bdd95e0ad;hb=7a341db1fbf5db94b711135b2a79e852c6fb1bc4;hp=0000000000000000000000000000000000000000;hpb=8050cacaa489292c82977b3d87971eaf9ca3c39b;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/mthread/main.c b/course/semester2/pprog/assignment1/mthread/main.c new file mode 100644 index 00000000..2d6b228b --- /dev/null +++ b/course/semester2/pprog/assignment1/mthread/main.c @@ -0,0 +1,64 @@ + +#include +#include +#include + +#include "nbody.h" +#include "graphics.h" + +unsigned numberOfProcessors; + +System universe; +pthread_t compute_thread; +pthread_mutex_t mutex_terminate; +bool terminate = false; + +void Cleanup_Threads(void); //Cleanup for threads + +// This is main function. Do not change it. +int main(int argc, char** argv) +{ + if (argc < 2) + { + printf("Please provide the filename, i.e. \'%s bodiesfield.dat\'\n", argv[0]); + exit(EXIT_FAILURE); + } + if (argc == 2) + { + System_Init(&universe,argv[1]); + atexit(Universe_Cleanup); + } + if (argc == 3) + { + numberOfProcessors = atoi(argv[2]); + } + + atexit(Cleanup_Threads); + + // Create a thread to handle computation loop + + if (pthread_create(&compute_thread, NULL, Compute_Thread, (void*)&universe) != 0) + { + perror("Error creating compute thread"); + exit(EXIT_FAILURE); + } + + Graphics_Run(argc, argv); // Run graphics loop in the main thread + + +} + +/** + * 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); +}