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. Multi-thread problem
Forum Updated to NodeBB v4.3 + New Features

Multi-thread problem

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

    Hi everyone:
    Currrently, I'm working with the project that was involved in the technology multi-thread. There is a promblem confused with me. it can be reprocude with minimal demo as the following code snippet:

    //test_a.h
    
    #include <QThread>
    #include <QObject>
    
    class Test_A: public QThread
    {
        Q_OBJECT
    public:
        static Test_A *GetInstance();
        ~Test_A();
        void run();
    
    public slots:
        void init();
        void exitCtl();
    
    private:
        Test_A()=default;
    
        bool bScaning = false;
    };
    
    //test_a.cpp
    
    Test_A* Test_A::GetInstance()
    {
        static Test_A obj;
        return &obj;
    }
    
    void Test_A::run()
    {
        bScaning = true;
        while (bScaning) {
        }
    }
    
    void Test_A::exitCtl()
    {
        bScaning = false;
    
    }
    
    

    It's well known that the function run will be invked in the another thread. Hence, Is there need for declaring the variable bScaning is of the type bool to std::atomic_bool? Additionally, Is there need for modifing the variable bScaning via std::atomic::store and std::atomic::load?

    Thanks for everyone who provide a feedback for this problem in advance!

    Christian EhrlicherC 1 Reply Last reply
    0
    • S Silent_Alex

      Hi everyone:
      Currrently, I'm working with the project that was involved in the technology multi-thread. There is a promblem confused with me. it can be reprocude with minimal demo as the following code snippet:

      //test_a.h
      
      #include <QThread>
      #include <QObject>
      
      class Test_A: public QThread
      {
          Q_OBJECT
      public:
          static Test_A *GetInstance();
          ~Test_A();
          void run();
      
      public slots:
          void init();
          void exitCtl();
      
      private:
          Test_A()=default;
      
          bool bScaning = false;
      };
      
      //test_a.cpp
      
      Test_A* Test_A::GetInstance()
      {
          static Test_A obj;
          return &obj;
      }
      
      void Test_A::run()
      {
          bScaning = true;
          while (bScaning) {
          }
      }
      
      void Test_A::exitCtl()
      {
          bScaning = false;
      
      }
      
      

      It's well known that the function run will be invked in the another thread. Hence, Is there need for declaring the variable bScaning is of the type bool to std::atomic_bool? Additionally, Is there need for modifing the variable bScaning via std::atomic::store and std::atomic::load?

      Thanks for everyone who provide a feedback for this problem in advance!

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

      @Silent_Alex said in Multi-thread problem:

      Hence, Is there need for declaring the variable bScaning is of the type bool to std::atomic_bool?

      Yes

      Additionally, Is there need for modifing the variable bScaning via std::atomic::store and std::atomic::load?

      No, assigning will be enough here.

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

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Silent_Alex said in Multi-thread problem:

        Hence, Is there need for declaring the variable bScaning is of the type bool to std::atomic_bool?

        Yes

        Additionally, Is there need for modifing the variable bScaning via std::atomic::store and std::atomic::load?

        No, assigning will be enough here.

        S Offline
        S Offline
        Silent_Alex
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thank, Bro! 😀

        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