Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112345

QtConcurrent::run() not always execute task in another thread

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.15.2, 6.4.2, 6.4.3
    • Core: QtConcurrent
    • None
    • Linux/X11

    Description

      There are two samples. The only difference is with line "x1.result()", first waits for result of QFuture before runing next task, second will wait for result after scheduling both tasks. I check if executed thread is in specified thread pool. In Test1() task from QtConcurrent::run() is executed in the main thread, not that from thread pool. This can lead to deadlocks or other unexpected behavior. This should work like in Test2(). Reproducible always.

      void MainWindow::Test1() {
      	QThreadPool fThreadPool;
      
      	qDebug() << "0 MAIN:" << fThreadPool.contains(QThread::currentThread()) << QThread::currentThread();
      
      	auto x1 = QtConcurrent::run(&fThreadPool, [&]() {
      		qDebug() << "1 POOL:" << fThreadPool.contains(QThread::currentThread()) << QThread::currentThread();
      		return 1;
      	});
      
      	qDebug() << "1 RESULT:" << x1.result();
      
      	auto x2 = QtConcurrent::run(&fThreadPool, [&]() {
      		qDebug() << "2 POOL:" << fThreadPool.contains(QThread::currentThread()) << QThread::currentThread();
      		return 2;
      	});
      
      	qDebug() << "2 RESULT:" << x2.result();
      }
      
      0 MAIN: false QThread(0x564b940bdc40)
      1 POOL: true QThreadPoolThread(0x564b941bb280, name = "Thread (pooled)")
      1 RESULT: 1
      2 POOL: false QThread(0x564b940bdc40)
      2 RESULT: 2
      
      void MainWindow::Test2() {
      	QThreadPool fThreadPool;
      
      	qDebug() << "0 MAIN:" << fThreadPool.contains(QThread::currentThread()) << QThread::currentThread();
      
      	auto x1 = QtConcurrent::run(&fThreadPool, [&]() {
      		qDebug() << "1 POOL:" << fThreadPool.contains(QThread::currentThread()) << QThread::currentThread();
      		return 1;
      	});
      
      	auto x2 = QtConcurrent::run(&fThreadPool, [&]() {
      		qDebug() << "2 POOL:" << fThreadPool.contains(QThread::currentThread()) << QThread::currentThread();
      		return 2;
      	});
      
      	qDebug() << "1 RESULT:" << x1.result();
      	qDebug() << "2 RESULT:" << x2.result();
      }
      
      0 MAIN: false QThread(0x5623f34e0c40)
      1 POOL: true QThreadPoolThread(0x5623f3565220, name = "Thread (pooled)")
      1 RESULT: 1
      2 POOL: true QThreadPoolThread(0x5623f36075e0, name = "Thread (pooled)")
      2 RESULT: 2
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              sonakur Sona Kurazyan
              slx SLX
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes