Skip to content
QtWS25 Call for Papers
  • 0 Votes
    14 Posts
    3k Views
    kshegunovK

    Humor me, will you?

    constexpr int poolSize = 16; QVector<QThread *> threadPool(poolSize); QCoreApplication * app = QCoreApplication::instance(); for (size_t i = 0; i < poolSize; i++) { QThread * thread = new QThread(app); thread->start(); QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater); } QObject::connect(app, &QCoreApplication::aboutToQuit, [threadPool] () -> void { for (QThread * thread : threadPool) { thread->quit(); thread->wait(); } }); int index = 0; for (const QString &video : videoFiles) { ThumbnailExtractor *extractor = new ThumbnailExtractor(counter, folderPath + '/' + video); extractor->moveToThread(threadPool[index++ % poolSize]); QObject::connect(extractor, &ThumbnailExtractor::finished, extractor, &QObject::deleteLater); QMetaObject::invokeMethod(extractor, &ThumbnailExtractor::generateThumbnail, Qt::QueuedConnection); }

    A note here:
    QMediaPlayer and related classes don't seem to be reentrant so you can't use them from different threads. Stick to the main one.

  • 0 Votes
    5 Posts
    500 Views
    Christian EhrlicherC

    @Dariusz said in QMutexLocker - locking question:

    So if its already locked, then it waits?

    This is exactly what a mutex is build for...

  • 0 Votes
    3 Posts
    1k Views
    S

    @SGaist Thank you!
    Now i see

    bool returnValue = d->wait(time); mutex->lock();

    It will wait for mutex after waking up.