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. Waiting for data in QQueue
QtWS25 Last Chance

Waiting for data in QQueue

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 2.1k Views
  • 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.
  • C Offline
    C Offline
    Clarck_D
    wrote on 20 Nov 2018, 11:06 last edited by Clarck_D
    #1

    Hi everyone !

    I want to wait on a QQueue to have data to treat and see if I get what I expect.
    For the waiting part, I tought about using a QtConcurrent::run, calling a function where I could do a while loop to check data in my queue. Also, I saw some code on StackOverflow wich inspired me this :

      //    /* Wait for reception */
    bool timeout = false;
    _timeout = 100;
    queueAckEmpty = queueAck.isEmpty();
    
    while (!timeout && queueAckEmpty){
        qCritical() << "Ack empty : " << queueAckEmpty;
        queueAckEmpty = queueAck.isEmpty();
        timeout = elapsedTime.elapsed() > _timeout;
        QApplication::processEvents(QEventLoop::AllEvents);
    }
    

    The problem with my code is that I would expect it to go out of the while loop as soon I have data in the queue but I don't and it print hundreds "Ack empty : ".

    I wanted to have your opinion on which is the best way to do this and why ?

    Thanks :)

    J 1 Reply Last reply 20 Nov 2018, 11:11
    0
    • C Clarck_D
      20 Nov 2018, 11:06

      Hi everyone !

      I want to wait on a QQueue to have data to treat and see if I get what I expect.
      For the waiting part, I tought about using a QtConcurrent::run, calling a function where I could do a while loop to check data in my queue. Also, I saw some code on StackOverflow wich inspired me this :

        //    /* Wait for reception */
      bool timeout = false;
      _timeout = 100;
      queueAckEmpty = queueAck.isEmpty();
      
      while (!timeout && queueAckEmpty){
          qCritical() << "Ack empty : " << queueAckEmpty;
          queueAckEmpty = queueAck.isEmpty();
          timeout = elapsedTime.elapsed() > _timeout;
          QApplication::processEvents(QEventLoop::AllEvents);
      }
      

      The problem with my code is that I would expect it to go out of the while loop as soon I have data in the queue but I don't and it print hundreds "Ack empty : ".

      I wanted to have your opinion on which is the best way to do this and why ?

      Thanks :)

      J Offline
      J Offline
      JonB
      wrote on 20 Nov 2018, 11:11 last edited by
      #2

      @Clarck_D
      If you are going to do it this way (you would be better with signal & slot), one processEvents() in a loop without delay is awfully busy. You would want some delay in the loop, like 1/100th or 1/1000th of a second to avoid too many calls.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on 20 Nov 2018, 11:24 last edited by
        #3

        If you write a little wrapper class around QQueue, you could use synchronization classes like QMutex:

        • Your queue offers a mutex
        • The mutex is locked while the queue is empty
        • The code processing the items of your queue wait for the mutex to unlock
        • When an item is added to the queue, the mutex is unlocked. As the last item is removed from the queue, the mutex is locked again.
        C 1 Reply Last reply 20 Nov 2018, 13:11
        3
        • A Asperamanca
          20 Nov 2018, 11:24

          If you write a little wrapper class around QQueue, you could use synchronization classes like QMutex:

          • Your queue offers a mutex
          • The mutex is locked while the queue is empty
          • The code processing the items of your queue wait for the mutex to unlock
          • When an item is added to the queue, the mutex is unlocked. As the last item is removed from the queue, the mutex is locked again.
          C Offline
          C Offline
          Clarck_D
          wrote on 20 Nov 2018, 13:11 last edited by
          #4

          I think I'm gonna try to use QtConcurrent and QMutex then. Thanks guys :)

          C 1 Reply Last reply 20 Nov 2018, 15:26
          0
          • C Clarck_D
            20 Nov 2018, 13:11

            I think I'm gonna try to use QtConcurrent and QMutex then. Thanks guys :)

            C Offline
            C Offline
            Clarck_D
            wrote on 20 Nov 2018, 15:26 last edited by
            #5

            Still I have a question, if I use QtConcurrent::run, the thread used will be released when function has finished ?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Nov 2018, 15:31 last edited by
              #6

              Hi,

              Looks like a case for QWaitCondition.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply 20 Nov 2018, 16:38
              1
              • S SGaist
                20 Nov 2018, 15:31

                Hi,

                Looks like a case for QWaitCondition.

                C Offline
                C Offline
                Clarck_D
                wrote on 20 Nov 2018, 16:38 last edited by
                #7

                @SGaist Ok so now I'm lost as for now I have no idea how to implement QtConcurrent::run, QMutex and QWaitCondition. The documentation is not really clear..

                K 1 Reply Last reply 20 Nov 2018, 23:08
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 20 Nov 2018, 21:17 last edited by
                  #8

                  You would use QtConcurrent once you have have data in your queue.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • C Clarck_D
                    20 Nov 2018, 16:38

                    @SGaist Ok so now I'm lost as for now I have no idea how to implement QtConcurrent::run, QMutex and QWaitCondition. The documentation is not really clear..

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 20 Nov 2018, 23:08 last edited by
                    #9

                    Queues and such:
                    https://forum.qt.io/topic/75880/proper-way-to-manage-information-between-threads

                    Read and abide by the Qt Code of Conduct

                    C 1 Reply Last reply 21 Nov 2018, 11:10
                    2
                    • K kshegunov
                      20 Nov 2018, 23:08

                      Queues and such:
                      https://forum.qt.io/topic/75880/proper-way-to-manage-information-between-threads

                      C Offline
                      C Offline
                      Clarck_D
                      wrote on 21 Nov 2018, 11:10 last edited by
                      #10

                      @SGaist No I can't because I want to use it for waiting data

                      I came across this :

                      However, QtConcurrent can't be used when communication with the running thread is needed, and it shouldn't be used to handle blocking operations.
                      

                      So I guess that get rid of Qtconcurrent for my purpose.
                      For now I went with the code I posted.

                      Still, thanks guys ;)

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Asperamanca
                        wrote on 21 Nov 2018, 11:58 last edited by
                        #11

                        There is mainly two use cases for multithreading:

                        1. Avoid blocking some other operation (often GUI)
                        2. Distribute work across multiple cores

                        QtConcurrent is good for 2) (especially if you need to perform an operation across multiple items in a container), but not really suited for 1)

                        1 Reply Last reply
                        0

                        1/11

                        20 Nov 2018, 11:06

                        • Login

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