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

Could slot function and mouseMoveEvent be possibly excuted concurrently in a muti-cores machine?



  • class MyScene : public QGraphicsScene
    {
        ...
    
        MyScene()
        {
            ...
    
            m_mouseEventEnabled = false;
            thread.start();
            connect(&thread, SIGNAL(finished()), this, SLOT(mouseEventEnabledSlot()));
    
            ...
        }
        
        ...
    
    public slots:
    
        void mouseEventEnabledSlot()
        {
            m_mouseEventEnabled = true;
        }
    
    protected:
    
        void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
        {
            if (m_mouseEventEnabled)
            {
                do_mouse_move_thing();
            }
        }
        
    private:
    
        bool m_mouseEventEnabled;
        MyThread thread;
    
        ...
    }
    

    Just as the code show, when the thread finishes, m_mouseEventEnabled will be set to true, for variable m_mouseEventEnabled, this is a writing behavior,

    but the m_mouseEventEnabled in function mouseMoveEvent may always do a reading behavior,

    The program will be run in a muti-cores machine, and what I want to know is that whether the two behaviors can possibly excute concurrently?

    ( I know little about the underlying implementation mechanism in Qt, but after I google the relevant articles, I think it will never happen, just my plain view).



  • change bool m_mouseEventEnabled; to std::atomic_bool m_mouseEventEnabled;. The rest should work



  • @VRonin What you mean is the two behaviors can be excuted concurrently ?


  • Qt Champions 2019

    @Limer said in Could slot function and mouseMoveEvent be possibly excuted concurrently in a muti-cores machine?:

    What you mean is the two behaviors can be excuted concurrently ?

    yes


  • Moderators

    Normally, a widget's slot functions and event functions are run in the GUI thread.

    It is possible to run function concurrently from mouseMoveEvent() by running the function in a different thread. However, it is best to implement that function in a different class (unless you really really know what you are doing)


Log in to reply