Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Synchronizing main thread with multiple QThread object finishes

$
0
0
I have a need to create checksums for each file in a folder. The QApplication (main) thread creates and starts the process using a pool of 8 array indexes for QThreads. Creating each checksum is performed in the run() function of each QThread. When each QThread finishes, and there are still more files to checksum, a new QThread is constructed, its pointer placed into the available array index, and is start()-ed (within the finish() handler) with a new file name. Problem: if I allow the above to run asynchronously with the main thread, all files’ checksums are created, and all threads correctly finish. But the main thread has no idea when this is completed. I would like to block (with cpu yielding) the main thread until all QThreads have finished. However, if I attempt to place any blocking code (based on QSemaphores, QWaitEvents or what have you), no thread finishes (i.e. the finish() slot is never entered—verified by posting a message if it is entered). There is no race condition (verified with system monitor). The main thread simply hangs and prevents finish() handling of any start()-ed QThread. There is no CPU utilization. Is there a way I can synchronize the main thread to wait for completion of all created/started QThreads? Here’s the code [main thread]:   if (currentImageCount > 0) //create image info for uncached images   {    for (t = 0; t < THREADS; t++)//initialize array of QThreads; THREADS is 8     pThread[t] = NULL;      finx = 0;      for (t = 0; t < THREADS; t++)    {     finxes[t] = finx;     if (finx < currentImageCount)     {        pThread[t] = new CreateChecksumThread(destinationPath + "/" + currentImageNames.at(finx), &(crcs[t]));        connect(pThread[t], SIGNAL(finished()), this, SLOT(slotSumFinished()));      pThread[t]->start();      finx++;     }    } // the above works fine until I attempt to put blocking/waiting/cpu yielding code here     } Here’s the finish() handler: slotSumFinished() // find out which thread finished for (t = 0; t < THREADS; t++) {  if (pThread[t] != NULL)  {   if (pThread[t]->isFinished())   {    disconnect(pThread[t], SIGNAL(finished()), 0, 0);    delete pThread[t];    pThread[t] = NULL;      checksums.append((qint64)crcs[t]);      if ((finxes[t] = finxes[t] + THREADS) < currentImageCount)    { // there are more files to checksum; reuse the index t in the array of QThreads      pThread[t] = new CreateChecksumThread(destinationPath /*imagePath*/ + "/" + currentImageNames.at(finxes[t]), &(crcs[t]));      connect(pThread[t], SIGNAL(finished()), this, SLOT(slotSumFinished()));      pThread[t]->start();     }     else     {      for (t = 0; t < THREADS; t++)      {       if (pThread[t] != NULL) // some QThread(s) are still running        return;      } // all threads have completed; here is where I would like  to wake up  the main thread }      }

Viewing all articles
Browse latest Browse all 13965

Trending Articles