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. QThread or QTimer advice
QtWS25 Last Chance

QThread or QTimer advice

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.8k 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.
  • S Offline
    S Offline
    sting
    wrote on last edited by
    #1

    I am trying to build a data acquisition application and I am struggling with the best way to capture the incoming data.

    The data arrives at a rate of 5 packets of 10K integers per second. My original idea was to just use a thread to check if data was available and if so get it to process and if not sleep for 20 ms. This locks up the qt event loop and is probably not such a good idea.

    Another alternative is to use a timer set to 20 ms that will check for data available. The problem I have thinking about this is that if I get data then I want to ignore several timer events while I process the current block of data. When I have processed the data I want to start getting timer events again. QTimer does not have a pause.

    I am thinking of a two stage approach where the first stage knows if a block is being processed and throws away the timer events and if a block is not being processed ask if data is available.

    Any suggestions would be appreciated.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Hi,

      First a question, how does a thread block your event loop? One of the reasons to use threads in the first place is not to block the event loop.

      In any case, what you could is have one thread put the data in a queue, and have another thread (or threads) process the data out of that queue.

      The first thread can use a simple timer, and if acquiring the data is fast you won't miss any events. The second thread (or threads) just take the data from the queue and processes it.

      Be sure to make your queue thread-safe and so on.

      Good luck.

      Ps. QTimer has a stop and restart function.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sting
        wrote on last edited by
        #3

        My thread function was:

        void Service::runIt(){
        while (isRunning){
        if (isStarted){
        acquire();
        process();
        }
        QThread::usleep(20);
        }
        }

        and I wanted to be able to turn on and off isStarted from two slots.
        void Service::startInput(){
        isStarted = true;
        }

        void Service::stopInput(){
        isStarted = false;
        }

        Because of the outer loop in runIt(), startIt() would never execute until I found the function and put it before the sleep.

        QCoreApplication::processEvents();

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          How are you using that class in your code ?

          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
          • S Offline
            S Offline
            sting
            wrote on last edited by
            #5

            The class has 3 slots that invoke runIt, startInput, and stopInput; The ui has two buttons that are connected to the startInput and stopInput slots. The thread starts the runIt method.

            The code in the outer class that hooks it all together is:
            @
            thread = new QThread;
            service = new Service();
            service->moveToThread(thread);
            connect(thread, SIGNAL(started()), service, SLOT(runIt()));
            connect(session, SIGNAL(startInput()), service, SLOT(startInput()));
            connect(session, SIGNAL(stopInput()), service, SLOT(stopInput()));
            connect(service, SIGNAL(writeData(int*)), session, SLOT(writeData(int*)));
            thread->start();
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Just thought of something, where are you data coming from ?

              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
              • S Offline
                S Offline
                sting
                wrote on last edited by
                #7

                I haven't connected the back end yet, but it is coming from a device that has a dll to provide access to the data. the flow of control I use is

                @
                int status = abcReadStatus();
                if (absIsFrameAvailable(){
                foo = abcReadFrame(false);
                writeData(foo); // a connection out of the thread for processing
                }
                QCoreApplication::processEvents();
                msleep(20);
                @

                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