Qt Forum

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

    Qt Academy Launch in California!

    QThread or QTimer advice

    General and Desktop
    3
    7
    1445
    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.
    • S
      sting last edited by

      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 Reply Quote 0
      • T
        t3685 last edited by

        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 Reply Quote 0
        • S
          sting last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • S
              sting last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • S
                  sting last edited by

                  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 Reply Quote 0
                  • First post
                    Last post