void tst_QThreadPool::waitForDoneTimeout() { class BlockedTask : public QRunnable { public: QMutex mutex; BlockedTask() { setAutoDelete(false); } void run() { mutex.lock(); mutex.unlock(); QTest::qSleep(50); } }; QThreadPool threadPool; BlockedTask *task = new BlockedTask; task->mutex.lock(); threadPool.start(task); QVERIFY(!threadPool.waitForDone(100)); task->mutex.unlock(); QVERIFY(threadPool.waitForDone(400)); }
void tst_QThreadPool::waitForDone() { QTime total, pass; total.start(); QThreadPool threadPool; while (total.elapsed() < 10000) { int runs; runs = count = 0; pass.restart(); while (pass.elapsed() < 100) { threadPool.start(new CountingRunnable()); ++runs; } threadPool.waitForDone(); QCOMPARE(int(count), runs); runs = count = 0; pass.restart(); while (pass.elapsed() < 100) { threadPool.start(new CountingRunnable()); threadPool.start(new CountingRunnable()); runs += 2; } threadPool.waitForDone(); QCOMPARE(int(count), runs); } }
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); Worker worker; worker.setAutoDelete(false); QThreadPool *threadPool = QThreadPool::globalInstance(); threadPool->start(&worker); qDebug() << "Hello from GUI thread. ID is " << (int)QThread::currentThreadId(); threadPool->waitForDone(); return 0; }
void tst_QThreadPool::tryStartCount() { class SleeperTask : public QRunnable { public: SleeperTask() { setAutoDelete(false); } void run() { QTest::qWait(50); } }; SleeperTask task; QThreadPool threadPool; const int runs = 5; for (int i = 0; i < runs; ++i) { // qDebug() << "iteration" << i; int count = 0; while (threadPool.tryStart(&task)) ++count; QCOMPARE(count, QThread::idealThreadCount()); QTest::qWait(100); } }
void QmitkMatchPointRegistrationManipulator::OnStoreBtnPushed() { mitk::MAPRegistrationWrapper::Pointer newRegWrapper = mitk::MAPRegistrationWrapper::New(); MAPRegistrationType::Pointer newReg = MAPRegistrationType::New(); newRegWrapper->SetRegistration(newReg); ::map::core::RegistrationManipulator<MAPRegistrationType> manipulator(newReg); ::map::core::PreCachedRegistrationKernel<3, 3>::Pointer kernel = ::map::core::PreCachedRegistrationKernel<3, 3>::New(); kernel->setTransformModel(m_InverseCurrentTransform); ::map::core::PreCachedRegistrationKernel<3, 3>::Pointer kernel2 = ::map::core::PreCachedRegistrationKernel<3, 3>::New(); kernel2->setTransformModel(m_InverseCurrentTransform->GetInverseTransform()); manipulator.setInverseMapping(kernel); manipulator.setDirectMapping(kernel2); if (this->m_Controls.radioSelectedReg->isChecked()) { //compine registration with selected pre registration as baseline typedef ::map::core::RegistrationCombinator<MAPRegistrationType, MAPRegistrationType> CombinatorType; CombinatorType::Pointer combinator = CombinatorType::New(); newReg = combinator->process(*m_SelectedPreReg,*newReg); newRegWrapper->SetRegistration(newReg); } mitk::DataNode::Pointer spResultRegistrationNode = mitk::generateRegistrationResultNode( this->m_Controls.lbNewRegName->text().toStdString(), newRegWrapper, "org.mitk::manual_registration", mitk::EnsureUID(m_SelectedMovingNode->GetData()), mitk::EnsureUID(m_SelectedTargetNode->GetData())); this->GetDataStorage()->Add(spResultRegistrationNode); if (m_Controls.checkMapEntity->checkState() == Qt::Checked) { QmitkMappingJob* pMapJob = new QmitkMappingJob(); pMapJob->setAutoDelete(true); pMapJob->m_spInputData = this->m_SelectedMovingNode->GetData(); pMapJob->m_InputDataUID = mitk::EnsureUID(m_SelectedMovingNode->GetData()); pMapJob->m_spRegNode = spResultRegistrationNode; pMapJob->m_doGeometryRefinement = false; pMapJob->m_spRefGeometry = this->m_SelectedTargetNode->GetData()->GetGeometry()->Clone().GetPointer(); pMapJob->m_MappedName = this->m_Controls.lbNewRegName->text().toStdString() + std::string(" mapped moving data"); pMapJob->m_allowUndefPixels = true; pMapJob->m_paddingValue = 100; pMapJob->m_allowUnregPixels = true; pMapJob->m_errorValue = 200; pMapJob->m_InterpolatorLabel = "Linear Interpolation"; pMapJob->m_InterpolatorType = mitk::ImageMappingInterpolator::Linear; connect(pMapJob, SIGNAL(Error(QString)), this, SLOT(OnMapJobError(QString))); connect(pMapJob, SIGNAL(MapResultIsAvailable(mitk::BaseData::Pointer, const QmitkMappingJob*)), this, SLOT(OnMapResultIsAvailable(mitk::BaseData::Pointer, const QmitkMappingJob*)), Qt::BlockingQueuedConnection); QThreadPool* threadPool = QThreadPool::globalInstance(); threadPool->start(pMapJob); }
void Task::start( Task::action action,function_t function ) { m_function = function ; m_action = action ; QThreadPool * thread = QThreadPool::globalInstance() ; thread->setMaxThreadCount( 10 ) ; thread->start( this ) ; }
void VESPERSRoperCCDDetector::loadImageFromFileImplementation(const QString &filename) { VESPERSRoperQRunnableImageLoader *runner = new VESPERSRoperQRunnableImageLoader(filename, imageData_.size()); runner->setAutoDelete(true); QThreadPool *threads = QThreadPool::globalInstance(); connect(runner, SIGNAL(imageData(QVector<int>)), this, SLOT(onImageDataChanged(QVector<int>))); threads->start(runner); }
void tst_QThreadPool::runTask() { QThreadPool manager; ran = false; manager.start(new TestTask()); // Hang if task is not runned. while (ran == false) QTest::qSleep(100); // no busy loop - this doesn't work with FIFO schedulers }
void tst_QThreadPool::runFunction() { { QThreadPool manager; testFunctionCount = 0; manager.start(createTask(noSleepTestFunction)); } QCOMPARE(testFunctionCount, 1); }
void tst_QThreadPool::waitcomplete() { testFunctionCount = 0; const int runs = 500; for (int i = 0; i < 500; ++i) { QThreadPool pool; pool.start(createTask(noSleepTestFunction)); } QCOMPARE(testFunctionCount, runs); }
/* Test that the ThreadManager destructor waits until all threads have completed. */ void tst_QThreadPool::destruction() { value = new int; QThreadPool *threadManager = new QThreadPool(); threadManager->start(new IntAccessor()); threadManager->start(new IntAccessor()); delete threadManager; delete value; value = 0; }
void tst_QThreadPool::expiryTimeout() { ExpiryTimeoutTask task; QThreadPool threadPool; threadPool.setMaxThreadCount(1); int expiryTimeout = threadPool.expiryTimeout(); threadPool.setExpiryTimeout(1000); QCOMPARE(threadPool.expiryTimeout(), 1000); // run the task threadPool.start(&task); QVERIFY(task.semaphore.tryAcquire(1, 10000)); QCOMPARE(task.runCount, 1); QVERIFY(!task.thread->wait(100)); // thread should expire QThread *firstThread = task.thread; QVERIFY(task.thread->wait(10000)); // run task again, thread should be restarted threadPool.start(&task); QVERIFY(task.semaphore.tryAcquire(1, 10000)); QCOMPARE(task.runCount, 2); QVERIFY(!task.thread->wait(100)); // thread should expire again QVERIFY(task.thread->wait(10000)); // thread pool should have reused the expired thread (instead of // starting a new one) QCOMPARE(firstThread, task.thread); threadPool.setExpiryTimeout(expiryTimeout); QCOMPARE(threadPool.expiryTimeout(), expiryTimeout); }
QLandmarkManagerEngineSqlite::~QLandmarkManagerEngineSqlite() { QThreadPool *threadPool = QThreadPool::globalInstance(); threadPool->waitForDone(); if (m_dbWatcher !=0) delete m_dbWatcher; QSqlDatabase::database(m_dbConnectionName).close(); QSqlDatabase::removeDatabase(m_dbConnectionName); }
void tst_QThreadPool::start() { const int runs = 1000; count.store(0); { QThreadPool threadPool; for (int i = 0; i< runs; ++i) { threadPool.start(new CountingRunnable()); } } QCOMPARE(count.load(), runs); }
void MainWindow::openAn2k() { QString file = QFileDialog::getOpenFileName(this, tr("qwsqviewer"), QString(), tr("AN2K images (*.an2)")); if (file.isEmpty()) return; _loader->setFile(file); QThreadPool *threadPool = QThreadPool::globalInstance(); threadPool->start(_loader); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); Work work; work.setAutoDelete(false); QThreadPool *threadPool = QThreadPool::globalInstance(); threadPool->start(&work); qDebug() << "hello from GUI thread " << QThread::currentThread(); threadPool->waitForDone(); }
//! Helper function that initiates the image processing algorithms. void MRIOpenCV::RunProcess() { QThreadPool *threadPool = QThreadPool::globalInstance(); for(int i=0; i < this->Processes->size(); i++){ cout << this->Processes->size(); MRIProcess * process= this->Processes->front(); this->Processes->pop(); process->run(); //threadPool->start(process); } threadPool->waitForDone(); }
void KisMemoryPoolTest::benchmarkAlloc() { QThreadPool pool; pool.setMaxThreadCount(NUM_THREADS); QBENCHMARK { for(qint32 i = 0; i < NUM_THREADS; i++) { KisAllocStressJob *job = new KisAllocStressJob(); pool.start(job); } pool.waitForDone(); } }
void QmitkMatchPoint::OnRegResultIsAvailable(mitk::MAPRegistrationWrapper::Pointer spResultRegistration, const QmitkRegistrationJob* pRegJob) { mitk::DataNode::Pointer spResultRegistrationNode = mitk::generateRegistrationResultNode( pRegJob->m_JobName, spResultRegistration, pRegJob->GetLoadedAlgorithm()->getUID()->toStr(), pRegJob->m_MovingDataUID, pRegJob->m_TargetDataUID); if (pRegJob->m_StoreReg) { m_Controls.m_teLog->append( QString("<b><font color='blue'> Storing registration object in data manager ... </font></b>")); this->GetDataStorage()->Add(spResultRegistrationNode); this->GetRenderWindowPart()->RequestUpdate(); } if (m_Controls.m_checkMapEntity->checkState() == Qt::Checked) { QmitkMappingJob* pMapJob = new QmitkMappingJob(); pMapJob->setAutoDelete(true); pMapJob->m_spInputData = pRegJob->m_spMovingData; pMapJob->m_InputDataUID = pRegJob->m_MovingDataUID; pMapJob->m_spRegNode = spResultRegistrationNode; pMapJob->m_doGeometryRefinement = false; pMapJob->m_spRefGeometry = pRegJob->m_spTargetData->GetGeometry()->Clone().GetPointer(); pMapJob->m_MappedName = pRegJob->m_JobName + std::string(" mapped moving data"); pMapJob->m_allowUndefPixels = true; pMapJob->m_paddingValue = 100; pMapJob->m_allowUnregPixels = true; pMapJob->m_errorValue = 200; pMapJob->m_InterpolatorLabel = "Linear Interpolation"; pMapJob->m_InterpolatorType = mitk::ImageMappingInterpolator::Linear; connect(pMapJob, SIGNAL(Error(QString)), this, SLOT(OnMapJobError(QString))); connect(pMapJob, SIGNAL(MapResultIsAvailable(mitk::BaseData::Pointer, const QmitkMappingJob*)), this, SLOT(OnMapResultIsAvailable(mitk::BaseData::Pointer, const QmitkMappingJob*)), Qt::BlockingQueuedConnection); connect(pMapJob, SIGNAL(AlgorithmInfo(QString)), this, SLOT(OnAlgorithmInfo(QString))); m_Controls.m_teLog->append( QString("<b><font color='blue'>Started mapping input data...</font></b>")); QThreadPool* threadPool = QThreadPool::globalInstance(); threadPool->start(pMapJob); }
void GDALParcelSplit::run() { cityblocks.resetReading(); OGRFeature *poFeature; this->counter_added = 0; QThreadPool pool; while( (poFeature = cityblocks.getNextFeature()) != NULL ) { char* geo; poFeature->GetGeometryRef()->exportToWkt(&geo); //Geo destroyed by worker ParcelSplitWorker * ps = new ParcelSplitWorker(poFeature->GetFID(), this, this->width, this->target_length, this->splitFirst, geo); pool.start(ps); } pool.waitForDone(); this->generated = counter_added; }
void FavIconTest::concurrentRequestsShouldWork() { const int numThreads = 3; QThreadPool tp; tp.setMaxThreadCount(numThreads); QVector<QFuture<QString>> futures(numThreads); for (int i = 0; i < numThreads; ++i) { futures[i] = QtConcurrent::run(&tp, getAltIconUrl); } QVERIFY(tp.waitForDone(60000)); const QString firstResult = futures.at(0).result(); for (int i = 1; i < numThreads; ++i) { QCOMPARE(futures.at(i).result(), firstResult); } QVERIFY(!QPixmap(firstResult).isNull()); }
void tst_QThreadPool::tryStart() { class WaitingTask : public QRunnable { public: QSemaphore semaphore; WaitingTask() { setAutoDelete(false); } void run() { semaphore.acquire(); count.ref(); } }; count.store(0); WaitingTask task; QThreadPool threadPool; for (int i = 0; i < threadPool.maxThreadCount(); ++i) { threadPool.start(&task); } QVERIFY(!threadPool.tryStart(&task)); task.semaphore.release(threadPool.maxThreadCount()); threadPool.waitForDone(); QCOMPARE(count.load(), threadPool.maxThreadCount()); }
void tst_QPointer::threadSafety() { QThread owner; owner.start(); QThreadPool pool; for (int i = 0; i < 300; i++) { QPointer<TestRunnable> task = new TestRunnable; task->setAutoDelete(true); task->moveToThread(&owner); pool.start(task); } pool.waitForDone(); owner.quit(); owner.wait(); }
void ConvolutionFilter::process(const QList<float> &matrix, int radius) { QThreadPool threadPool; threadPool.setMaxThreadCount(16); int height = image.height(); for (int y = 0; y < height; y++) { ConvolutionFilterRunner *runner = new ConvolutionFilterRunner(&image); runner->setMatrix(matrix, radius); runner->setScanLine((QRgb *) processed.scanLine(y), y); runner->setAutoDelete(true); threadPool.start(runner); } printf("waiting for convolution filter..."); threadPool.waitForDone(); }
void KisPaintDeviceTest::testCacheState() { TestingCache cache; QList<CacheStressJob*> jobsList; CacheStressJob *job; for(qint32 i = 0; i < NUM_THREADS; i++) { //job = new CacheStressJob(value, cacheValue, cacheState); job = new CacheStressJob(cache); job->setAutoDelete(true); jobsList.append(job); } QThreadPool pool; pool.setMaxThreadCount(NUM_THREADS); foreach(job, jobsList) { pool.start(job); }
explicit Polygonizer(feature::GenerateInfo const & info) : m_info(info) #if PARALLEL_POLYGONIZER , m_ThreadPoolSemaphore(m_ThreadPool.maxThreadCount() * 8) #endif { #if PARALLEL_POLYGONIZER LOG(LINFO, ("Polygonizer thread pool threads:", m_ThreadPool.maxThreadCount())); #endif if (info.m_splitByPolygons) { CHECK(borders::LoadCountriesList(info.m_targetDir, m_countries), ("Error loading country polygons files")); } else { // Insert fake country polygon equal to whole world to // create only one output file which contains all features m_countries.Add(borders::CountryPolygons(info.m_fileName), MercatorBounds::FullRect()); } }
void tst_QThreadPool::setMaxThreadCount() { QFETCH(int, limit); QThreadPool *threadPool = QThreadPool::globalInstance(); int savedLimit = threadPool->maxThreadCount(); // maxThreadCount() should always return the previous argument to // setMaxThreadCount(), regardless of input threadPool->setMaxThreadCount(limit); QCOMPARE(threadPool->maxThreadCount(), limit); // the value returned from maxThreadCount() should always be valid input for setMaxThreadCount() threadPool->setMaxThreadCount(savedLimit); QCOMPARE(threadPool->maxThreadCount(), savedLimit); // setting the limit on children should have no effect on the parent { QThreadPool threadPool2(threadPool); savedLimit = threadPool2.maxThreadCount(); // maxThreadCount() should always return the previous argument to // setMaxThreadCount(), regardless of input threadPool2.setMaxThreadCount(limit); QCOMPARE(threadPool2.maxThreadCount(), limit); // the value returned from maxThreadCount() should always be valid input for setMaxThreadCount() threadPool2.setMaxThreadCount(savedLimit); QCOMPARE(threadPool2.maxThreadCount(), savedLimit); } }
void tst_QThreadPool::runMultiple() { const int runs = 10; { QThreadPool manager; testFunctionCount = 0; for (int i = 0; i < runs; ++i) { manager.start(createTask(sleepTestFunctionMutex)); } } QCOMPARE(testFunctionCount, runs); { QThreadPool manager; testFunctionCount = 0; for (int i = 0; i < runs; ++i) { manager.start(createTask(noSleepTestFunctionMutex)); } } QCOMPARE(testFunctionCount, runs); { QThreadPool manager; for (int i = 0; i < 500; ++i) manager.start(createTask(emptyFunct)); } }
/* Test that the thread pool really reuses threads. */ void tst_QThreadPool::threadRecycling() { QThreadPool threadPool; threadPool.start(new ThreadRecorderTask()); threadRecyclingSemaphore.acquire(); QThread *thread1 = recycledThread; QTest::qSleep(100); threadPool.start(new ThreadRecorderTask()); threadRecyclingSemaphore.acquire(); QThread *thread2 = recycledThread; QCOMPARE(thread1, thread2); QTest::qSleep(100); threadPool.start(new ThreadRecorderTask()); threadRecyclingSemaphore.acquire(); QThread *thread3 = recycledThread; QCOMPARE(thread2, thread3); }
void tst_QThreadPool::tryStartPeakThreadCount() { class CounterTask : public QRunnable { public: CounterTask() { setAutoDelete(false); } void run() { { QMutexLocker lock(&mutex); ++activeThreads; peakActiveThreads = qMax(peakActiveThreads, activeThreads); } QTest::qWait(100); { QMutexLocker lock(&mutex); --activeThreads; } } }; CounterTask task; QThreadPool threadPool; for (int i = 0; i < 20; ++i) { if (threadPool.tryStart(&task) == false) QTest::qWait(10); } QCOMPARE(peakActiveThreads, QThread::idealThreadCount()); for (int i = 0; i < 20; ++i) { if (threadPool.tryStart(&task) == false) QTest::qWait(10); } QCOMPARE(peakActiveThreads, QThread::idealThreadCount()); }