Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

No eventLoop and direct connection



  • 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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



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


Log in to reply