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. Qt Thread architecture
Forum Updated to NodeBB v4.3 + New Features

Qt Thread architecture

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 1.1k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    king558
    wrote on last edited by king558
    #1

    Hello Qt Gurus,

    I have following process scenario. I need receive datas from server A, parse the content and put the datas to server B.

    I itend to have two thread, the main thread with gui to communicate with server B and another thread communicate with server A. The following is pseudo-code

    class ServerAThread: public QThread {
              QMutex _mutex;
             QString   _data;
             QLocalServer _server;
             void run { QMutexLocker locker( &_mutex );  _server.receive( data ); _data = data;  }
    public:
             ServerAThread( QString &data ) : _data( data ) {}
    };
    
    class MainWindow : public QMainWindow {
            QString               _data; // shared data between Thread A and Main
            ServerAThread _threadA;
    
    MainWindow() : _threadA( _data ) { _threadA.start(); }
    }
    

    Is this architechture great or do you have better idea for this? Do I need a loop to check any change of datas or is better to use emit signal? Do emit Signal block the thread A if the main thread need time to parse the content of data?

    SGaistS 1 Reply Last reply
    0
    • K king558

      Hello Qt Gurus,

      I have following process scenario. I need receive datas from server A, parse the content and put the datas to server B.

      I itend to have two thread, the main thread with gui to communicate with server B and another thread communicate with server A. The following is pseudo-code

      class ServerAThread: public QThread {
                QMutex _mutex;
               QString   _data;
               QLocalServer _server;
               void run { QMutexLocker locker( &_mutex );  _server.receive( data ); _data = data;  }
      public:
               ServerAThread( QString &data ) : _data( data ) {}
      };
      
      class MainWindow : public QMainWindow {
              QString               _data; // shared data between Thread A and Main
              ServerAThread _threadA;
      
      MainWindow() : _threadA( _data ) { _threadA.start(); }
      }
      

      Is this architechture great or do you have better idea for this? Do I need a loop to check any change of datas or is better to use emit signal? Do emit Signal block the thread A if the main thread need time to parse the content of data?

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The first question is: why do you think you need threads involved ? Qt as well as its networking classes are asynchronous. Take advantage of that first. If the load is so heavy that it triggers freezing issues in your GUI then, and only then, add a thread in the mix.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        The first question is: why do you think you need threads involved ? Qt as well as its networking classes are asynchronous. Take advantage of that first. If the load is so heavy that it triggers freezing issues in your GUI then, and only then, add a thread in the mix.

        K Offline
        K Offline
        king558
        wrote on last edited by king558
        #3

        @SGaist

        Hello, I forgot to mentions about communication with Server A using third libraries, so it need a loop, check in every loop the data is coming in. So there must a thread for the communication with Server A.

        SGaistS K 2 Replies Last reply
        0
        • K king558

          @SGaist

          Hello, I forgot to mentions about communication with Server A using third libraries, so it need a loop, check in every loop the data is coming in. So there must a thread for the communication with Server A.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well, QLocalServer is also asynchronous. Which 3rd party library do you need to use ? You are showing none in your code sample.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K king558

            @SGaist

            Hello, I forgot to mentions about communication with Server A using third libraries, so it need a loop, check in every loop the data is coming in. So there must a thread for the communication with Server A.

            K Offline
            K Offline
            king558
            wrote on last edited by
            #5

            @king558

            its a tdlib on github, from its I take datas, it runs with a loop, check its state and give the content to main thread to parse the content and forward to Server B

            Christian EhrlicherC 1 Reply Last reply
            0
            • K king558

              @king558

              its a tdlib on github, from its I take datas, it runs with a loop, check its state and give the content to main thread to parse the content and forward to Server B

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

              The provided code does nothing - at least what not you expect. It neither shares some data nor is the mutex useful at all. Receive the data, emit a signal and connect it to a slot in your main thread if you need a separate thread at all - your example code does not.

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

              SGaistS K 2 Replies Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                The provided code does nothing - at least what not you expect. It neither shares some data nor is the mutex useful at all. Receive the data, emit a signal and connect it to a slot in your main thread if you need a separate thread at all - your example code does not.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                tdlib ? Are you writing a client for telegram ? If so, there are Qt based libraries for that which might be simpler to use in your case.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #8

                  It is not good style (anymore) to override QThread::run(). Have a look at the documentation for QThread how to create a worker thread.

                  There is an even better approach for your use case. Just create a plain old QThread object and start it. It will run its own event loop. You should probably not run an infinite loop to takes up every CPU cycle of one core. Instead, start a QTimer (with a low timeout) in this thread to call your external library and check for new data.

                  Also, try to avoid QMutex. Synchronization between threads is slow. Furthermore, in your small code example only a single object has access to the mutex. This means that you don't need it.

                  K 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    The provided code does nothing - at least what not you expect. It neither shares some data nor is the mutex useful at all. Receive the data, emit a signal and connect it to a slot in your main thread if you need a separate thread at all - your example code does not.

                    K Offline
                    K Offline
                    king558
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher

                    Really? Could you please provide me the name or github link?

                    SGaistS 2 Replies Last reply
                    0
                    • K king558

                      @Christian-Ehrlicher

                      Really? Could you please provide me the name or github link?

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @king558 where you in fact asking me for a link ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      K 1 Reply Last reply
                      0
                      • S SimonSchroeder

                        It is not good style (anymore) to override QThread::run(). Have a look at the documentation for QThread how to create a worker thread.

                        There is an even better approach for your use case. Just create a plain old QThread object and start it. It will run its own event loop. You should probably not run an infinite loop to takes up every CPU cycle of one core. Instead, start a QTimer (with a low timeout) in this thread to call your external library and check for new data.

                        Also, try to avoid QMutex. Synchronization between threads is slow. Furthermore, in your small code example only a single object has access to the mutex. This means that you don't need it.

                        K Offline
                        K Offline
                        king558
                        wrote on last edited by
                        #11

                        @SimonSchroeder said in Qt Thread architecture:

                        It is not good style (anymore) to override QThread::run(). Have a look at the documentation for QThread how to create a worker thread.

                        There is an even better approach for your use case. Just create a plain old QThread object and start it. It will run its own event loop. You should probably not run an infinite loop to takes up every CPU cycle of one core. Instead, start a QTimer (with a low timeout) in this thread to call your external library and check for new data.

                        Also, try to avoid QMutex. Synchronization between threads is slow. Furthermore, in your small code example only a single object has access to the mutex. This means that you don't need it.

                        Thx you, I have a infinite run loop, that need to check the state of the connection and data received. In this case is movetoThread also possible or override QThread::run is best fot fit?

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          @king558 where you in fact asking me for a link ?

                          K Offline
                          K Offline
                          king558
                          wrote on last edited by
                          #12

                          @SGaist said in Qt Thread architecture:

                          @king558 where you in fact asking me for a link ?

                          You said, there are qt based library for telegram, so I asked you for the name if it is third party library and where I can find it

                          1 Reply Last reply
                          0
                          • K king558

                            @Christian-Ehrlicher

                            Really? Could you please provide me the name or github link?

                            SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @king558 said in Qt Thread architecture:

                            @Christian-Ehrlicher

                            Really? Could you please provide me the name or github link?

                            I don't want to nitpick but you literally tagged @Christian-Ehrlicher with that request hence my question.

                            Anyway, the first hit when searching Qt telegram client brings you this https://github.com/Kaffeine/telegram-qt

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            K 1 Reply Last reply
                            1
                            • SGaistS SGaist

                              @king558 said in Qt Thread architecture:

                              @Christian-Ehrlicher

                              Really? Could you please provide me the name or github link?

                              I don't want to nitpick but you literally tagged @Christian-Ehrlicher with that request hence my question.

                              Anyway, the first hit when searching Qt telegram client brings you this https://github.com/Kaffeine/telegram-qt

                              K Offline
                              K Offline
                              king558
                              wrote on last edited by
                              #14

                              @SGaist

                              Thx you for link, it was my mistake.

                              1 Reply Last reply
                              0
                              • K king558 has marked this topic as solved on

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved