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. Is communicating with a std::jthread via a std::atomic<bool> flag set by slots safe?
Forum Updated to NodeBB v4.3 + New Features

Is communicating with a std::jthread via a std::atomic<bool> flag set by slots safe?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 575 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.
  • A Offline
    A Offline
    andyP
    wrote on last edited by andyP
    #1

    When watching a recorded online presentation named "Multithreading with Qt" by Giuseppe D’Angelo I notice that he recommends to avoid adding slots to QThread.
    In the example he uses (at 50 minutes) his QThread has a Socket member variable that he creates in the run() of his thread. Not sure which class Socket actually is meant to be, but I am guessing QTcpSocket.
    His thread has a slot and this will be called from the main thread since a QThread has affinity with the main thread (when it is created there).
    I understand that this is a problem since QTcpSocket is reentrant but not thread safe.
    I guess the same goes for QSerialPort? I should never use these in more than one thread?

    In my code I have a class like this:

    class Thread : public QObject {
    	Q_OBJECT
    public:
    	Thread(); // will start threadFunction() in m_thread
    public slots:
    	void start() {
    		m_running = true;
    	}
    	void stop() {
    		m_running = false;
    	}
    private:
    	void threadFunction(); // runs a forever loop and if m_running do some work
    	std::atomic<bool> m_running{ false };
    	std::jthread m_thread;
    };
    

    The slots start() and stop() will be called from another non main thread. Thread is created in the main thread.

    Is this safe?

    Christian EhrlicherC 1 Reply Last reply
    0
    • A andyP

      When watching a recorded online presentation named "Multithreading with Qt" by Giuseppe D’Angelo I notice that he recommends to avoid adding slots to QThread.
      In the example he uses (at 50 minutes) his QThread has a Socket member variable that he creates in the run() of his thread. Not sure which class Socket actually is meant to be, but I am guessing QTcpSocket.
      His thread has a slot and this will be called from the main thread since a QThread has affinity with the main thread (when it is created there).
      I understand that this is a problem since QTcpSocket is reentrant but not thread safe.
      I guess the same goes for QSerialPort? I should never use these in more than one thread?

      In my code I have a class like this:

      class Thread : public QObject {
      	Q_OBJECT
      public:
      	Thread(); // will start threadFunction() in m_thread
      public slots:
      	void start() {
      		m_running = true;
      	}
      	void stop() {
      		m_running = false;
      	}
      private:
      	void threadFunction(); // runs a forever loop and if m_running do some work
      	std::atomic<bool> m_running{ false };
      	std::jthread m_thread;
      };
      

      The slots start() and stop() will be called from another non main thread. Thread is created in the main thread.

      Is this safe?

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @andyP said in Is communicating with a std::jthread via a std::atomic<bool> flag set by slots safe?:

      Is this safe?

      Yes, but it's safe even without any Qt.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @andyP said in Is communicating with a std::jthread via a std::atomic<bool> flag set by slots safe?:

        Is this safe?

        Yes, but it's safe even without any Qt.

        A Offline
        A Offline
        andyP
        wrote on last edited by
        #3

        @Christian-Ehrlicher: thank you for your help!

        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