Parallel Programming - Finished(?) pthread version
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / main.c
1
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <pthread.h>
5
6 #include "nbody.h"
7 #include "graphics.h"
8
9 unsigned numberOfProcessors;
10
11 System universe;
12 pthread_t compute_thread;
13 pthread_mutex_t mutex_runstate;
14 RUNSTATE runstate = RUN;
15
16 void Thread_Cleanup(void)
17 {
18         if (runstate == RUN)
19                 QuitProgram(false);
20         pthread_join(compute_thread, NULL);
21 }
22
23 // This is main function. Do not change it.
24 int main(int argc, char** argv)
25 {
26         if (argc < 2) 
27         {
28                 printf("Please provide the filename, i.e. \'%s bodiesfield.dat\'\n", argv[0]);
29                 exit(EXIT_FAILURE);
30         }
31         if (argc == 2)
32         {
33                 System_Init(&universe,argv[1]);
34                 atexit(Universe_Cleanup);
35         }
36         if (argc == 3) 
37         {
38                 numberOfProcessors = atoi(argv[2]);
39         }
40         
41         atexit(Thread_Cleanup);
42         
43         // Create a thread to handle computation loop
44         
45         if (pthread_create(&compute_thread, NULL, Compute_Thread, (void*)&universe) != 0)
46         {
47                 perror("Error creating compute thread");
48                 exit(EXIT_FAILURE);
49         }
50         
51         Graphics_Run(argc, argv); // Run graphics loop in the main thread
52         
53         printf("Close!\n");
54         //We get to this point when the "close" button is clicked in the graphics window
55         QuitProgram(false);
56         pthread_join(compute_thread, NULL);
57         
58 }
59

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