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 my multithread application proper?
Forum Updated to NodeBB v4.3 + New Features

Is my multithread application proper?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 740 Views 1 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.
  • E Offline
    E Offline
    eoreyg
    wrote on last edited by eoreyg
    #1

    Hello. I designed an application which contains multithtread, and messaging over TCP in a 1-5 ms time period. But I'm new to multithread stuffs. I would appreciate if you could look over it if it is a suitable use.

    -Getting received data:

    /*TcpServer class source file*/
    TcpServer::TcpServer
    {
            MessageParser *parser = new MessageParser;
    
            parser->moveToThread(&m_parserThread);
        
            connect(this, SIGNAL(parseSignal(MessagePkg )),
                    parser, SLOT(parseMessage(MessagePkg )));
    }
    
    /*ReadyRead signal of my socket object is connected to receiveProcess method*/
    
    TcpServer::receiveProcess()
    {
                     quint16 bytes_available = m_socket->readAll();
                     
                      /* Some stuffs to push the data inside of  bytes_available to my message package*/
                     ...
    
                     /* Switching to thread process to parsing message package*/
                     m_parserThread.start();
                     
                     emit parseSignal(msg_pkg);
    }
    
    

    In parseMessage method, I'm only assigning the appropriate bytes to my global variables which are shown in the Gui side. At the other side, I'm refreshing the variables on the screen with a timer which has 500ms time period. I guess it is clear to understand. What do you think about my design, is it correct approach for multithreading.

    What if my parseMessage(MessagePkg) method like this:

    MessageParser::parseMessage(MessagePkg) 
    {
                   /* Some parsing stuffs*/
                    ...
    
                    emit doLogSignal(MessagePkg);    /* For logging message package */
    }
    

    Normally, I think I'm starting the thread after every package is ready in Tcp server, and thread is finishing '}' of parseMessage method, right? What if I emit a signal for logging, what will be lifetime of the thread? Same way, Will the thread be finished at '}' of parseMessage, after the log slot is completed?

    Thanks a lot.

    Pl45m4P 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You should and must not start the thread during receive. Start them in the ctor. Then it's eventloop is running and the slot can be executed.
      Stop the thread in your dtor later on.

      Also readAll() is probably not what you want to use.

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

      1 Reply Last reply
      1
      • E Offline
        E Offline
        eoreyg
        wrote on last edited by eoreyg
        #3

        Thank you. But, my TcpServer object alive along the program lifecycle. So will it enter the dtor, if I want to stop the thread in the dtor? Will the thread not finish with the end of the my thread slot method? Do I need m_parserThread->quit to finish the thread.

        Am I starting during receive? I was thinking that I'm starting thread after receive operation is finished?

        Another thing, what should I use instead of readAll(). Could you open a little bit more, please? I could not get it exactly.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @eoreyg said in Is my multithread application proper?:

          ill the thread not finish with the end of the my thread slot method?

          No, at least not until you somehow stop them which I doubt you do. Please read the docs about QThread.

          wrt: readAll() - readyRead() is called every time bytes are available. Therefore you have to parse your stream to find your packet inside the stream.

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

          1 Reply Last reply
          5
          • E eoreyg

            Hello. I designed an application which contains multithtread, and messaging over TCP in a 1-5 ms time period. But I'm new to multithread stuffs. I would appreciate if you could look over it if it is a suitable use.

            -Getting received data:

            /*TcpServer class source file*/
            TcpServer::TcpServer
            {
                    MessageParser *parser = new MessageParser;
            
                    parser->moveToThread(&m_parserThread);
                
                    connect(this, SIGNAL(parseSignal(MessagePkg )),
                            parser, SLOT(parseMessage(MessagePkg )));
            }
            
            /*ReadyRead signal of my socket object is connected to receiveProcess method*/
            
            TcpServer::receiveProcess()
            {
                             quint16 bytes_available = m_socket->readAll();
                             
                              /* Some stuffs to push the data inside of  bytes_available to my message package*/
                             ...
            
                             /* Switching to thread process to parsing message package*/
                             m_parserThread.start();
                             
                             emit parseSignal(msg_pkg);
            }
            
            

            In parseMessage method, I'm only assigning the appropriate bytes to my global variables which are shown in the Gui side. At the other side, I'm refreshing the variables on the screen with a timer which has 500ms time period. I guess it is clear to understand. What do you think about my design, is it correct approach for multithreading.

            What if my parseMessage(MessagePkg) method like this:

            MessageParser::parseMessage(MessagePkg) 
            {
                           /* Some parsing stuffs*/
                            ...
            
                            emit doLogSignal(MessagePkg);    /* For logging message package */
            }
            

            Normally, I think I'm starting the thread after every package is ready in Tcp server, and thread is finishing '}' of parseMessage method, right? What if I emit a signal for logging, what will be lifetime of the thread? Same way, Will the thread be finished at '}' of parseMessage, after the log slot is completed?

            Thanks a lot.

            Pl45m4P Online
            Pl45m4P Online
            Pl45m4
            wrote on last edited by
            #5

            @eoreyg said in Is my multithread application proper?:

            Normally, I think I'm starting the thread after every package is ready in Tcp server, and thread is finishing '}' of parseMessage method, right? What if I emit a signal for logging, what will be lifetime of the thread? Same way, Will the thread be finished at '}' of parseMessage, after the log slot is completed?

            Normally you start/stop the thread using signals & slots.

            Will the thread not finish with the end of the my thread slot method? Do I need m_parserThread->quit to finish the thread.

            If your MessageParser is done with its work (in your case, when you stop your TCP connection), you connect for example MessageParser::done with QThread::quit.

            If you want to stop it earlier and start a new thread, you have to do it.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            Christian EhrlicherC E 2 Replies Last reply
            1
            • Pl45m4P Pl45m4

              @eoreyg said in Is my multithread application proper?:

              Normally, I think I'm starting the thread after every package is ready in Tcp server, and thread is finishing '}' of parseMessage method, right? What if I emit a signal for logging, what will be lifetime of the thread? Same way, Will the thread be finished at '}' of parseMessage, after the log slot is completed?

              Normally you start/stop the thread using signals & slots.

              Will the thread not finish with the end of the my thread slot method? Do I need m_parserThread->quit to finish the thread.

              If your MessageParser is done with its work (in your case, when you stop your TCP connection), you connect for example MessageParser::done with QThread::quit.

              If you want to stop it earlier and start a new thread, you have to do it.

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

              @Pl45m4 I don't see why the thread should be stopped here at all. The code doesn't show any hints for it.

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

              Pl45m4P 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Pl45m4 I don't see why the thread should be stopped here at all. The code doesn't show any hints for it.

                Pl45m4P Online
                Pl45m4P Online
                Pl45m4
                wrote on last edited by
                #7

                @Christian-Ehrlicher said in Is my multithread application proper?:

                I don't see why the thread should be stopped here at all

                Yes, but as far as I understand, @eoreyg wants to start/stop the thread after the processing of every result (for whatever reason)...


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                Christian EhrlicherC 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @eoreyg said in Is my multithread application proper?:

                  Normally, I think I'm starting the thread after every package is ready in Tcp server, and thread is finishing '}' of parseMessage method, right? What if I emit a signal for logging, what will be lifetime of the thread? Same way, Will the thread be finished at '}' of parseMessage, after the log slot is completed?

                  Normally you start/stop the thread using signals & slots.

                  Will the thread not finish with the end of the my thread slot method? Do I need m_parserThread->quit to finish the thread.

                  If your MessageParser is done with its work (in your case, when you stop your TCP connection), you connect for example MessageParser::done with QThread::quit.

                  If you want to stop it earlier and start a new thread, you have to do it.

                  E Offline
                  E Offline
                  eoreyg
                  wrote on last edited by
                  #8

                  @Pl45m4 Thank you. Yes I want to start the thread with a packet came. After stopping thread, starting again the thread in receiveProcess instead of constructor of TcpServer is OK, or still bad approach?

                  Pl45m4P 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @Christian-Ehrlicher said in Is my multithread application proper?:

                    I don't see why the thread should be stopped here at all

                    Yes, but as far as I understand, @eoreyg wants to start/stop the thread after the processing of every result (for whatever reason)...

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

                    @Pl45m4 But then the code above will not work since the signal may not be executed (since the thread or better the eventloop may not run yet then). It's just wrong and useless.

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

                    1 Reply Last reply
                    5
                    • E eoreyg

                      @Pl45m4 Thank you. Yes I want to start the thread with a packet came. After stopping thread, starting again the thread in receiveProcess instead of constructor of TcpServer is OK, or still bad approach?

                      Pl45m4P Online
                      Pl45m4P Online
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @eoreyg said in Is my multithread application proper?:

                      still bad approach?

                      Definitely :)

                      Why you want to restart the thread every time?

                      Start your thread in your constructor or while you initalize everything, pass your worker to your thread and then just let it keep running. You can communicate with your worker and thread using signals&slots, as I said before (they are asynchronous). Your log-Signal looks fine.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      E 1 Reply Last reply
                      2
                      • Pl45m4P Pl45m4

                        @eoreyg said in Is my multithread application proper?:

                        still bad approach?

                        Definitely :)

                        Why you want to restart the thread every time?

                        Start your thread in your constructor or while you initalize everything, pass your worker to your thread and then just let it keep running. You can communicate with your worker and thread using signals&slots, as I said before (they are asynchronous). Your log-Signal looks fine.

                        E Offline
                        E Offline
                        eoreyg
                        wrote on last edited by eoreyg
                        #11

                        @Pl45m4 @Christian-Ehrlicher It was clear. Thanks a lot

                        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