Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Equivalent to boost::condition_variable
Forum Updated to NodeBB v4.3 + New Features

Equivalent to boost::condition_variable

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 1.6k Views 2 Watching
  • 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.
  • jsulmJ jsulm

    @SPlatten There is https://doc.qt.io/qt-5/qwaitcondition.html or if you want C++ stdlib there is https://en.cppreference.com/w/cpp/thread/condition_variable

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #4

    @jsulm , I'm trying to implement QWaitCondition, is there an equivalent to exit the wait ?

    Kind Regards,
    Sy

    jsulmJ JKSHJ 2 Replies Last reply
    0
    • SPlattenS SPlatten

      @jsulm , I'm trying to implement QWaitCondition, is there an equivalent to exit the wait ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @SPlatten I'm not sure what you mean. There are notify* and wake* methods.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #6

        The original class has a method:

        void className::waitForServerReady() {
            boost::unique_lock<boost::mutex> lock(*this);
            m_serverReadyCondition.wait(lock, [this]() { return m_serverReady; });
        }
        

        I've replaced the boost::unique_lock and boost::mutex with:

        QMutexLocker lock(&m_mutex);
        

        Where m_mutex is now defined as:

        mutable QMutex m_mutex;
        

        And in the class constructor:

        m_mutex(QMutex::Recursive)
        

        In the original class, m_serverReadyCondition is defined as:

        boost::condition_variable m_serverReadyCondition;
        

        Which I've replaced with:

        QWaitCondition m_serverReadyCondition;
        

        m_serverReady is a bool.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • SPlattenS SPlatten

          @jsulm , I'm trying to implement QWaitCondition, is there an equivalent to exit the wait ?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #7

          @SPlatten said in Equivalent to boost::condition_variable:

          I'm trying to implement QWaitCondition, is there an equivalent to exit the wait ?

          ...

          void className::waitForServerReady() {
              boost::unique_lock<boost::mutex> lock(*this);
              m_serverReadyCondition.wait(lock, [this]() { return m_serverReady; });
          }
          

          The "exit" function is called the predicate.

          QWaitCondition doesn't accept a predicate, but you can implement your own checks in a while loop: https://stackoverflow.com/questions/33692574/stdcondition-variablewait-with-predicate

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          SPlattenS 1 Reply Last reply
          2
          • JKSHJ JKSH

            @SPlatten said in Equivalent to boost::condition_variable:

            I'm trying to implement QWaitCondition, is there an equivalent to exit the wait ?

            ...

            void className::waitForServerReady() {
                boost::unique_lock<boost::mutex> lock(*this);
                m_serverReadyCondition.wait(lock, [this]() { return m_serverReady; });
            }
            

            The "exit" function is called the predicate.

            QWaitCondition doesn't accept a predicate, but you can implement your own checks in a while loop: https://stackoverflow.com/questions/33692574/stdcondition-variablewait-with-predicate

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #8

            @JKSH , In the class there is a method to modify the boolean m_serverReady, when this is set to true it also calls:

            m_serverReadyCondition.notify_all();
            

            Will that be enough?

            Kind Regards,
            Sy

            JKSHJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JKSH , In the class there is a method to modify the boolean m_serverReady, when this is set to true it also calls:

              m_serverReadyCondition.notify_all();
              

              Will that be enough?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #9

              @SPlatten said in Equivalent to boost::condition_variable:

              @JKSH , In the class there is a method to modify the boolean m_serverReady, when this is set to true it also calls:

              m_serverReadyCondition.notify_all();
              

              Will that be enough?

              That's not enough. You should check the predicate to avoid a lost wakeup which can cause a deadlock: https://www.modernescpp.com/index.php/c-core-guidelines-be-aware-of-the-traps-of-condition-variables

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              SPlattenS 1 Reply Last reply
              2
              • JKSHJ JKSH

                @SPlatten said in Equivalent to boost::condition_variable:

                @JKSH , In the class there is a method to modify the boolean m_serverReady, when this is set to true it also calls:

                m_serverReadyCondition.notify_all();
                

                Will that be enough?

                That's not enough. You should check the predicate to avoid a lost wakeup which can cause a deadlock: https://www.modernescpp.com/index.php/c-core-guidelines-be-aware-of-the-traps-of-condition-variables

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #10

                @JKSH , all those examples are using std::condition_variable which looks like a close match to the boost::condition_variable.

                I can't see a similar way of doing the same with QWaitCondition.

                Kind Regards,
                Sy

                JKSHJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @JKSH , all those examples are using std::condition_variable which looks like a close match to the boost::condition_variable.

                  I can't see a similar way of doing the same with QWaitCondition.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #11

                  @SPlatten said in Equivalent to boost::condition_variable:

                  @JKSH , all those examples are using std::condition_variable which looks like a close match to the boost::condition_variable.

                  I can't see a similar way of doing the same with QWaitCondition.

                  QWaitCondition behaves just like std::condition_variable and boost::condition_variable. The only difference is that QWaitCondition::wait() doesn't take a lambda.

                  Look closely at the examples again. How can you replace the lambda with a while loop?

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  SPlattenS 1 Reply Last reply
                  1
                  • JKSHJ JKSH

                    @SPlatten said in Equivalent to boost::condition_variable:

                    @JKSH , all those examples are using std::condition_variable which looks like a close match to the boost::condition_variable.

                    I can't see a similar way of doing the same with QWaitCondition.

                    QWaitCondition behaves just like std::condition_variable and boost::condition_variable. The only difference is that QWaitCondition::wait() doesn't take a lambda.

                    Look closely at the examples again. How can you replace the lambda with a while loop?

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #12

                    @JKSH , so for example if I take:

                    m_serverReadyCondition.wait(lock, [this]() { return m_serverReady; });
                    

                    And replace with:

                    while( !m_serverReady ) {
                        m_serverReadyCondition.wait(lock);
                    }
                    

                    Would that be ok?

                    Kind Regards,
                    Sy

                    JKSHJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @JKSH , so for example if I take:

                      m_serverReadyCondition.wait(lock, [this]() { return m_serverReady; });
                      

                      And replace with:

                      while( !m_serverReady ) {
                          m_serverReadyCondition.wait(lock);
                      }
                      

                      Would that be ok?

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #13

                      @SPlatten said in Equivalent to boost::condition_variable:

                      @JKSH , so for example if I take:

                      m_serverReadyCondition.wait(lock, [this]() { return m_serverReady; });
                      

                      And replace with:

                      while( !m_serverReady ) {
                          m_serverReadyCondition.wait(lock);
                      }
                      

                      Would that be ok?

                      Looks good to me

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved