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) { int count = 0; while (threadPool.tryStart(&task)) ++count; QCOMPARE(count, QThread::idealThreadCount()); QTest::qWait(100); } }
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()); }
static PyObject *meth_QThreadPool_tryStart(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QRunnable* a0; PyObject *a0Wrapper; QThreadPool *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B@J8", &sipSelf, sipType_QThreadPool, &sipCpp, &a0Wrapper, sipType_QRunnable, &a0)) { bool sipRes = 0; #line 53 "/Users/Kunwiji/Dropbox/Spectroscopy_paper/PyQt-mac-gpl-4.11.2/sip/QtCore/qthreadpool.sip" // We have to handle the object ownership manually. if (a0->autoDelete()) sipTransferTo(a0Wrapper, sipSelf); Py_BEGIN_ALLOW_THREADS sipRes = sipCpp->tryStart(a0); Py_END_ALLOW_THREADS #line 362 "/Users/Kunwiji/Dropbox/Spectroscopy_paper/PyQt-mac-gpl-4.11.2/QtCore/sipQtCoreQThreadPool.cpp" return PyBool_FromLong(sipRes); } }
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()); }