MainWindow::~MainWindow() { delete ui; CleanThreads(); scene->removeItem((QGraphicsItem*)pixmapItem); delete pixmapItem; delete scene; }
void MainWindow::StartCalculation(bool julian_set) { int i; QSize size(viewWidth,viewHeight); QPointF coordinates = ViewCentreCoordinates; double pixel_size = pixelSize; Effect effect = SelectedEffect; /* Clean possible old threads */ CleanThreads(); /* Start Threading */ CPUAmount = QThread::idealThreadCount(); ThreadList = new QThread*[CPUAmount]; ThreadObjList = new PixelCalculationThread*[CPUAmount]; /* In case julian preview mode then tune few parameters */ if (julian_set) { size.setWidth(julianWidth); effect = JulianSetEffect; coordinates = JulianViewCentreCoordinates; pixel_size = JulianpixelSize; } /* Calculate optimal cluster size */ cluster_size = size.width()/CPUAmount; /* Initialize where first completed thread calculation should start */ next_x = 0; next_y = cluster_size; ThreadsRunning = 0; /* Create threads and start calculate first vawe of pitmap data */ for (i= 0; i < CPUAmount;i++) { ThreadObjList[i] = new PixelCalculationThread(cluster_size*i,0,cluster_size,pixel_size,&size,&coordinates,effect,colorBoost); ThreadList[i] = new QThread(); ThreadObjList[i]->setup(ThreadList[i],i); ThreadObjList[i]->moveToThread(ThreadList[i]); connect(ThreadObjList[i],SIGNAL(CalculationDone(int)),this,SLOT(ThreadCompleted(int))); ThreadList[i]->start(); ThreadsRunning++; } /* Calculate total threads needed */ threads_needed = 0; threads_needed = (size.height()/cluster_size)*CPUAmount; if (size.height()%cluster_size) { threads_needed +=CPUAmount; } ui->progressBar->setValue(0); clusterId = CPUAmount; }
BOOL CSocketThreadManager::Uninitialize() { try { //Are we initialized? if (!m_bInitialized) return TRUE; //Are we a static class if (m_bStaticClass) //Deallocate the class UnregisterClass(); else //Delete the thread data CleanThreads(FALSE); //Unitialized m_bInitialized=FALSE; //Done return TRUE; } ERROR_HANDLER_RETURN("Uninitialize",FALSE) }
BOOL CSocketThreadManager::SpawnThreads(LPCSTR lpClassName) { try { //Add to this class our thread ID, and a random number if (!lpClassName) { //Calculate our string int iStringLength; iStringLength=strlen(CSocketThreadManager_Class); //Add some size to it iStringLength+=50; //Allocate the string std::auto_ptr<char> pString(new char[iStringLength]); //Get our thread ID DWORD dwThreadID; dwThreadID=GetCurrentThreadId(); //Get the tick of the system DWORD dwProcessID; dwProcessID=GetTickCount(); //Create the string sprintf(pString.get(),"%s_%lu_%lu",CSocketThreadManager_Class, GetCurrentThreadId(), GetTickCount()); //And register the window //Check if we need to register the class if (!m_bRegisteredWindow) if (!RegisterClass(pString.get())) { //Report it ReportError("SpawnThreads","Failed to register window!"); //And quit return FALSE; } //Save the class name m_sClassName=pString.get(); } else m_sClassName=lpClassName; //Start creating threads //Allocate the thread structure m_pThreadData=new ThreadData[m_ulThreadCount]; //Initialize the data for (int iCount=0;iCount<m_ulThreadCount;++iCount) { m_pThreadData[iCount].bFreeze=FALSE; m_pThreadData[iCount].pEvent=NULL; m_pThreadData[iCount].hInstance=NULL; m_pThreadData[iCount].hWindowHandle=NULL; m_pThreadData[iCount].iSocketCount=0; m_pThreadData[iCount].iTimeoutCount=0; m_pThreadData[iCount].pThread=NULL; m_pThreadData[iCount].pCSection=COSManager::CreateCriticalSection(); m_pThreadData[iCount].pClass=this; } //Did we have an error? BOOL bError; bError=FALSE; //Our waiting list CWaitList aList; //Start spawning for (int iCounter=0; iCounter<m_ulThreadCount; ++iCounter) { //Copy the class name m_pThreadData[iCounter].sClassName=m_sClassName; //Create an event m_pThreadData[iCounter].pEvent=COSManager::CreateEvent(); //Add it to the list aList.AddObject(m_pThreadData[iCounter].pEvent, TRUE); //Set our instance m_pThreadData[iCounter].hInstance=m_hInstance; //Create extended data CreateExtendedData(m_pThreadData[iCounter].aExtension); //And create it m_pThreadData[iCounter].pThread=COSManager::CreateThread(SocketThread); //Check the thread has been created if (!m_pThreadData[iCounter].pThread->GetThreadID()) { //Report the error ReportError("SpawnThreads","Failed to create thread!"); //We have an error bError=TRUE; //Exit the loop break; } else //Start the thread m_pThreadData[iCounter].pThread->Start((LPVOID)&m_pThreadData[iCounter]); } //Tmp position DWORD dwTmp; if (aList.Wait(TRUE, dwTmp, THREADS_TIMEOUT)) { //Report the error ReportError("SpawnThreads","Timeout waiting for threads!"); //Close the threads CleanThreads(TRUE); //Exit return FALSE; } else if (bError) { //Close the threads CleanThreads(FALSE); //Exit return FALSE; } else { //We are OK m_bInitialized=TRUE; //Done return TRUE; } } ERROR_HANDLER_RETURN("SpawnThreads",FALSE) }