+ Barrier_Join(&graphics_barrier);
+
+}
+
+#ifdef PERSISTENT_THREADS
+
+/**
+ * @function Worker_Thread
+ * @purpose Thread - A self contained worker thread to compute a particular sub system of bodies
+ *
+ * This is the "smart" way to do it, because threads are only created once, and compute both force and position.
+ * The greatest difficulty with pthreads is getting a *single* thread from the team to execute certain code
+ * (ie: The stuff in StepFunction()).
+ * With the "continuously respawning threads of stupidity" approach,
+ * because there is one "master" thread (not necessarilly the main thread... don't get confused now)
+ * to keep respawning the workers, the single threaded code can just be executed in the master thread.
+ *
+ * With this approach, I have created a hacky solution so that the *last* thread to leave the position barrier gets to call StepFunction.
+ *
+ */
+void * Worker_Thread(void * arg)
+{
+ System * s = (System*)(arg); // This is mainly to save typing the RHS a lot of times
+
+ // Each thread runs until the whole program is supposed to end
+ while (!ExitCondition())
+ {
+
+
+ System_Forces(s, &universe); // Each thread computes the forces for its share of bodies
+
+ // Do not confuse with "Barrier_Wait".
+ // Barrier_Wait does not affect the barrier; it just waits for it
+ // Barrier_Join actively updates the state of the barrier, and wakes up sleeping threads if required.
+
+ Barrier_Join(&force_barrier); // All threads must reach here before moving on.
+ if (ExitCondition()) return NULL;
+
+
+ //fprintf(stderr,"Thread %p - force barrier finished\n", arg);
+ //printf("Computed ALL forces\n");
+
+
+ // If required, wait for the graphics to finish drawing stuff
+ if (options.draw_graphics && options.pedantic_graphics)
+ {
+ //printf("Worker %p waits on graphics barrier\n", arg);
+ Barrier_Wait(&graphics_barrier);
+ //printf("\tWorker %p wakes up after graphics barrier\n", arg);
+ if (ExitCondition()) return NULL;
+ }
+
+
+
+ Barrier_Enter(&position_barrier);
+ System_Positions(s); // Each thread updates the positions for its share of bodies
+
+
+ // Barrier_JoinCall behaves in the same way as Barrier_Join, except the *last* thread
+ // (ie: the one that wakes up the others) also calls the function with arguments given.
+ Barrier_JoinCall(&position_barrier, StepFunction, (void*)(&universe));
+ if (ExitCondition()) return NULL;
+ //Barrier_Join(&position_barrier);
+
+ // All threads have computed positions, and *one* thread calls StepFunction()
+
+ }
+ QuitProgram(false); // Set the run state of the program
+ return NULL;