Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED]QMutex not working at same way on Windows and Linux

    General and Desktop
    2
    7
    4418
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      antonyprojr last edited by

      I'm developing a code to run in a separate thread for testing and i'm using a QMutexLocker to lock it
      code:

      @
      QFuture<void>t1;
      QMutex mutex;
      int num;

      void MainWindow::thread1()
      {
      QMutexLocker locker(&mutex);
      locker.mutex()->lock();
      num++;
      qDebug() << "Thread unlocked " + QString::number(num);
      }

      void MainWindow::on_pushButton_clicked()
      {
      if (!t1.isRunning())
      {
      t1 = QtConcurrent::run(this, &MainWindow::thread1);
      }
      }

      void MainWindow::on_pushButton_2_clicked()
      {
      mutex.unlock();
      }
      @

      The problem is: if I call push Button 2 before call push Button 1, in other words, if I unlock the thread before start it, on Windows i get: "Thread unlocked" but on Linux nothing happens. Why? And how to do the QMutex work at the same way both Windows and Linux?

      1 Reply Last reply Reply Quote 0
      • M
        MuldeR last edited by

        The problem may be in this code:
        @ QMutexLocker locker(&mutex);
        locker.mutex()->lock(); //Not needed!@

        The constructor of QMutexLocker will already lock the mutex, no need to lock a mutex you already locked!

        Also, from what I can see, the "main" thread never locked the mutex, so what is on_pushButton_2_clicked() supposed to do? Only a thread that has locked (taken ownership of) a mutex can unlock that mutex again...

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply Reply Quote 0
        • A
          antonyprojr last edited by

          What you mean? I can't use QMutex across threads? If yes is there any way to lock/unlock some thread from another thread? Because is it that i'm looking for.

          1 Reply Last reply Reply Quote 0
          • M
            MuldeR last edited by

            What I mean is: You can use a mutex across multiple threads. Different threads can try to lock the same mutex. If thread A tries to lock the mutex, while the mutex already is locked by thread B, then thread A will be blocked at this point until thread B unlocks the mutex. But: A can not unlock a mutex that is currently locked/owned by B.

            bq. void QMutex::unlock ()
            Unlocks the mutex. Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior.

            So you are probably looking for a (binary) semaphore or a conditional variable here...

            See also:
            http://doc.qt.digia.com/qt/qsemaphore.html

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply Reply Quote 0
            • A
              antonyprojr last edited by

              QSemaphore can be used across threads without any problem? If yes my question is solved!

              1 Reply Last reply Reply Quote 0
              • M
                MuldeR last edited by

                Yes, the thread which acquire()'s a Semaphore does not take ownership, in the sense of a Mutex. If the Semaphore is zero while one thread calls acquire(), it will get blocked. Another thread can then call release() on the Semaphore to unblock the waiting thread. The thread who calls release() does not need to have called acquire() before. In contrast to a conditional variable, the Semaphore will "remember" the release() if no thread is currently waiting, so the next acquire() won't block. With a conditional variable, if you call wakeOne() while no other thread is waiting, the "wake" gets lost! Depends on your individual problem which one you need.

                Typical example for use of Semaphores is the "consumer/producer" pattern:
                http://en.wikipedia.org/wiki/Semaphore_(programming)#Example:_Producer.2Fconsumer_problem

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply Reply Quote 0
                • A
                  antonyprojr last edited by

                  Thanks, your reply is very helpful!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post