+
+ if (runstate != RUN) pthread_exit(NULL); //Check whether the main thread wants to exit
+
+
+
+ #if NUM_THREADS <= 1
+ //No worker threads; do everything in this thread
+ System_Forces(s, s);
+ System_Positions(s);
+ #else
+
+
+
+
+ worker_count = NUM_THREADS; //All threads working
+
+ //Compute forces
+ for (unsigned i = 0; i < NUM_THREADS; ++i)
+ {
+ if (pthread_create(worker_thread+i, &attr, Force_Thread, (void*)(sub_system+i)) != 0)
+ {
+ perror("In compute thread, couldn't create worker thread (force)");
+ QuitProgram(true);
+ pthread_exit(NULL);
+ }
+ }
+
+
+
+ //Wait for forces to be computed
+ pthread_mutex_lock(&mutex_workers);
+ while (worker_count > 0)
+ pthread_cond_wait(&workers_done_cv, &mutex_workers);
+ pthread_mutex_unlock(&mutex_workers);
+
+ worker_count = NUM_THREADS; //All threads working
+
+ //Compute positions
+ for (unsigned i = 0; i < NUM_THREADS; ++i)
+ {
+ if (pthread_create(worker_thread+i, &attr, Position_Thread, (void*)(sub_system+i)) != 0)
+ {
+ perror("In compute thread, couldn't create worker thread (position)");
+ QuitProgram(true);
+ pthread_exit(NULL);
+ }
+ }
+
+ //Wait for positions to be computed
+ pthread_mutex_lock(&mutex_workers);
+ while (worker_count > 0)
+ pthread_cond_wait(&workers_done_cv, &mutex_workers);
+ pthread_mutex_unlock(&mutex_workers);
+
+
+ #endif
+