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. No eventLoop and direct connection

No eventLoop and direct connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 590 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.
  • D Offline
    D Offline
    Drazz
    wrote on last edited by Drazz
    #1

    Hello,

    I'm developing a real time system with Qt. I've got a worker without eventLoop that can receive a slot from other thread (just one) with Qt::DirectConnection that change the state machine status of the RealTimeWorker.

    class RealTimeWorker : public QObject
    {
    public:
        RealTimeWorker();
    
        enum status_enum {
            idle,
            running,
            sleep
        };
    
    public slots:
    
        void run() {
    
            while (!QThread::currentThread()->isInterruptionRequested()) {
    
                switch (status) {
                case idle:
                    // do idle
                    break;
    
                case running:
                    // do work
                    break;
    
                case sleep:
                    // do sleep
                    break;
                }
            }
        }
    
        void handleStatusChange(status_enum value) {
            status = value;
        }
    
    
    private:
        status_enum status;
    
    };
    

    I'm wondering if this can cause any runtime problems. The emiter thread only writes in status and this object only reads. If possible, I would like to know where I can find more info about this.

    Thanks in advice

    jsulmJ 1 Reply Last reply
    0
    • D Drazz

      Hello,

      I'm developing a real time system with Qt. I've got a worker without eventLoop that can receive a slot from other thread (just one) with Qt::DirectConnection that change the state machine status of the RealTimeWorker.

      class RealTimeWorker : public QObject
      {
      public:
          RealTimeWorker();
      
          enum status_enum {
              idle,
              running,
              sleep
          };
      
      public slots:
      
          void run() {
      
              while (!QThread::currentThread()->isInterruptionRequested()) {
      
                  switch (status) {
                  case idle:
                      // do idle
                      break;
      
                  case running:
                      // do work
                      break;
      
                  case sleep:
                      // do sleep
                      break;
                  }
              }
          }
      
          void handleStatusChange(status_enum value) {
              status = value;
          }
      
      
      private:
          status_enum status;
      
      };
      

      I'm wondering if this can cause any runtime problems. The emiter thread only writes in status and this object only reads. If possible, I would like to know where I can find more info about this.

      Thanks in advice

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

      @Drazz said in No eventLoop and direct connection:

      while (QThread::currentThread()->isInterruptionRequested())

      This condition is wrong.

      Be aware that direct connection means that handleStatusChange will be executed in the other thread (not the worker thread)! This will lead to race conditions.

      Also I'm wondering what exactly you mean with "real time"? Do you really have hard requirements? If not you could simply use a thread with event loop and queued connections.

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

      D 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Drazz said in No eventLoop and direct connection:

        while (QThread::currentThread()->isInterruptionRequested())

        This condition is wrong.

        Be aware that direct connection means that handleStatusChange will be executed in the other thread (not the worker thread)! This will lead to race conditions.

        Also I'm wondering what exactly you mean with "real time"? Do you really have hard requirements? If not you could simply use a thread with event loop and queued connections.

        D Offline
        D Offline
        Drazz
        wrote on last edited by
        #3

        @jsulm said in No eventLoop and direct connection:

        Also I'm wondering what exactly you mean with "real time"? Do you really have hard requirements? If not you could simply use a thread with event loop and queued connections.

        I need to change hardware outputs given some sensors inputs every sample time.

        Except for status change, all data is transfered with producer-consumer.

        jsulmJ 1 Reply Last reply
        0
        • D Drazz

          @jsulm said in No eventLoop and direct connection:

          Also I'm wondering what exactly you mean with "real time"? Do you really have hard requirements? If not you could simply use a thread with event loop and queued connections.

          I need to change hardware outputs given some sensors inputs every sample time.

          Except for status change, all data is transfered with producer-consumer.

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

          @Drazz Do you really need a thread for that? How much work is this thread actually doing?

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

          D 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Drazz Do you really need a thread for that? How much work is this thread actually doing?

            D Offline
            D Offline
            Drazz
            wrote on last edited by Drazz
            #5

            @jsulm said in No eventLoop and direct connection:

            @Drazz Do you really need a thread for that? How much work is this thread actually doing?

            Yes! It's computing images from 3 RGB cameras. One thread takes images next does the analysis and next writes outputs. I'm not using signals and slots to have control of queues sizes.

            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