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
Forum Updated to NodeBB v4.3 + New Features

Waiting for data in QQueue

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 2.4k Views 3 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.
  • C Offline
    C Offline
    Clarck_D
    wrote on 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 :)

    JonBJ 1 Reply Last reply
    0
    • C Clarck_D

      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 :)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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 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
        3
        • A Asperamanca

          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 last edited by
          #4

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

          C 1 Reply Last reply
          0
          • C Clarck_D

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

            C Offline
            C Offline
            Clarck_D
            wrote on 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              1
              • SGaistS SGaist

                Hi,

                Looks like a case for QWaitCondition.

                C Offline
                C Offline
                Clarck_D
                wrote on 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..

                kshegunovK 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 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

                    @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..

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on 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
                    2
                    • kshegunovK kshegunov

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

                      C Offline
                      C Offline
                      Clarck_D
                      wrote on 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 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

                        • Login

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