void EGPlanner::run() { mMultiThread = true; mRenderType = RENDER_NEVER; //threaded planners always use cloned hands createAndUseClone(); //signal that initialization is ready setState(INIT); threadLoop(); }
void NSFThread::threadEntry(const NSFContext&) { try { threadLoop(); } catch(const std::exception& exception) { std::runtime_error newException(getName() + " thread loop exception: " + exception.what()); handleException(newException); } catch(...) { std::runtime_error newException(getName() + " thread loop exception: unknown exception"); handleException(newException); } LOCK(getThreadMutex()) { NSFEnvironment::getEnvironment().removeThread(this); terminationStatus = ThreadTerminated; } ENDLOCK; }
/* Sets up and runs the threads */ void relaxationThreaded(float** inArray, float** outArray, int arraySize, float precision, int numThreads) { if (numThreads > 0) { // Calculate granularity int currPos = 0; // plus 2 because we want to overlap and edges are // kept constant by the functions. int threadChunk = (arraySize / numThreads) + 2; // To store the data for the thread function LoopData loopDataArray[numThreads]; pthread_t threads[numThreads]; // All threads must reach the barrier before we continue pthread_barrier_t theBarrier; pthread_barrier_init(&theBarrier, NULL, numThreads); // Shared counter so processes know when they're finished int finishedThreads = 0; // Lock for the counter pthread_mutex_t theLock; pthread_mutex_init(&theLock, NULL); // Loop and create/start threads int i; for ( i = 0; i < numThreads; i++) { // The last chunk is a bit of a special case if ( i == (numThreads - 1) ) { int columnsRemaining = (arraySize - currPos); loopDataArray[i].arrayX = columnsRemaining; threadChunk = columnsRemaining; } else // It shouldn't be possible for any chunk but the last to go { // OOB, so keep that assumption (maybe bad practice, but // protections higher up should hide it) loopDataArray[i].arrayX = threadChunk; } loopDataArray[i].inArray = &inArray[currPos]; loopDataArray[i].outArray = &outArray[currPos]; loopDataArray[i].arrayY = arraySize; loopDataArray[i].precision = precision; loopDataArray[i].barrier = &theBarrier; loopDataArray[i].numThreads = numThreads; loopDataArray[i].finishedThreads= &finishedThreads; loopDataArray[i].finLock = &theLock; if (__VERBOSE) { printf("Starting thread %d.\n", i); } pthread_create(&threads[i], NULL, threadLoop, (void*)&loopDataArray[i]); currPos += threadChunk - 2; } // join will block if the thread is going, otherwise doesn't block. for( i = 0; i < numThreads; i++) { pthread_join(threads[i], NULL); } // Cleanup pthread_barrier_destroy(&theBarrier); pthread_mutex_destroy(&theLock); } else { //== Serial Computation == LoopData data; int finishedThreads = 0; data.inArray = inArray; data.outArray = outArray; data.arrayX = arraySize; data.arrayY = arraySize; data.precision = precision; data.barrier = NULL; data.finLock = NULL; data.numThreads = 1; data.finishedThreads = &finishedThreads; threadLoop((void*)&data); } }