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

Can't acquire semaphore in the function called by QtConcurrent::map

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P1: Critical
    • 5.11.0 Beta 3
    • 5.11.0 Beta 1
    • Core: Threads
    • None
    • Linux
    • 081c001deb75fa38385d3ff8cbbdb4f788529133

    Description

      Hello,

      I want to report a problem about using a semaphore in the function called by QtConcurrent::map. The problem could be reproduced by the following code: 

      QSemaphore semaphore(1);
      
      void worker(int value) {
          qDebug() << value << "entry";
          semaphore.acquire();
          semaphore.release();
          qDebug() << value << "exit";
      }
      
      void SemaphoreTest::bug() // A unit test function
      {
          int count = 4;
          QList<int> input;
          for (int i = 0 ;i < count;i++) {
              input << i;
          }
      
          semaphore.acquire();
      
          QFuture<void> future = QtConcurrent::map(input, worker);
      
          QEventLoop loop;
          QTimer::singleShot(1000, [&]() {
              loop.quit();
          });
          loop.exec();
      
          qDebug() << "release semaphore in main thread";
          semaphore.release();
      
          QTRY_COMPARE(future.isFinished(), true);
      }
      
      

       The expected output : 

      // The order of number is not important
      QDEBUG : SemaphoreTest::bug() 3 entry
      QDEBUG : SemaphoreTest::bug() 0 entry
      QDEBUG : SemaphoreTest::bug() 2 entry
      QDEBUG : SemaphoreTest::bug() 1 entry
      QDEBUG : SemaphoreTest::bug() release semaphore in main thread
      QDEBUG : SemaphoreTest::bug() 0 exit
      QDEBUG : SemaphoreTest::bug() 3 exit
      QDEBUG : SemaphoreTest::bug() 2 exit
      QDEBUG : SemaphoreTest::bug() 1 exit
      PASS : SemaphoreTest::bug()
      

       However, the actual output in Qt 5.11 on Ubuntu Linux (17.10) is 

      QDEBUG : SemaphoreTest::bug() 0 entry
      QDEBUG : SemaphoreTest::bug() 1 entry
      QDEBUG : SemaphoreTest::bug() 2 entry
      QDEBUG : SemaphoreTest::bug() 3 entry
      QDEBUG : SemaphoreTest::bug() release semaphore in main thread
      QDEBUG : SemaphoreTest::bug() 0 exit
      FAIL! : SemaphoreTest::bug() Compared values are not the same
      Actual (((future.isFinished()))): 0
      Expected (true) : 1
      Loc: [../semaphore/tst_semaphoretest.cpp(52)]
      PASS : SemaphoreTest::cleanupTestCase()
      
      

      Only the first function is able to acquire the semaphore. The rest are failed and never wake up. That won't happen for Qt 5.10 and previous versions. I have tested it with Qt 5.11 + Mac too. But it works as expected. So that may be a Linux only bug.

      I have attached an example project to reproduce the bug for your reference.

      Thanks.

       

       

       

       

       

      Attachments

        Activity

          People

            thiago Thiago Macieira
            benlau Ben Lau
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: