--- /dev/null
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <pthread.h>
+
+#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);
+}