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. can't emit a signal from a Thread that is inside of another Thread in QT(GUI)
Forum Updated to NodeBB v4.3 + New Features

can't emit a signal from a Thread that is inside of another Thread in QT(GUI)

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 6 Posters 1.3k 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.
  • S solidtyper

    @jsulm said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

    l exec() i

    how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #8

    @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

    how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.

    Multithreading is always complicated task, are you really sure you need multi-thread?

    If you want to use multi-threading, then avoid to specialize the QThread class but create a QObject based worker class and use QObject::moveToThread() to move the class instance to the desired thread.
    This is the Qt recommanded way.

    Take a look at this article which explaint it much better as I can do:

    • https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
    • https://www.vikingsoftware.com/blog/how-to-use-qthread-properly/

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    S 1 Reply Last reply
    2
    • KroMignonK KroMignon

      @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

      how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.

      Multithreading is always complicated task, are you really sure you need multi-thread?

      If you want to use multi-threading, then avoid to specialize the QThread class but create a QObject based worker class and use QObject::moveToThread() to move the class instance to the desired thread.
      This is the Qt recommanded way.

      Take a look at this article which explaint it much better as I can do:

      • https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
      • https://www.vikingsoftware.com/blog/how-to-use-qthread-properly/
      S Offline
      S Offline
      solidtyper
      wrote on last edited by solidtyper
      #9

      @KroMignon i found a video of KDAB "Multithreading with Qt (Part 3) - QThread with an event loop".

      that talks about event loop where he moves the object to the thread inside the constructor of the object and clean up in the deconstructor.

      i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.

      JonBJ KroMignonK 2 Replies Last reply
      0
      • S solidtyper

        @KroMignon i found a video of KDAB "Multithreading with Qt (Part 3) - QThread with an event loop".

        that talks about event loop where he moves the object to the thread inside the constructor of the object and clean up in the deconstructor.

        i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #10

        @solidtyper
        Just an observation: if you're going to have thread sleep and poll every 50 milliseconds, you can presumably do same with QTimer without needing a thread for that.

        i'm very beginner

        My suggestion is if you can avoid threads, do so.

        S 1 Reply Last reply
        1
        • S solidtyper

          @KroMignon i found a video of KDAB "Multithreading with Qt (Part 3) - QThread with an event loop".

          that talks about event loop where he moves the object to the thread inside the constructor of the object and clean up in the deconstructor.

          i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #11

          @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

          i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.

          You are looking in the wrong direction ;)
          If you want to use QObject and signals/slots, you must have a working event loop, because it is there where the signals will be processed (cf. https://doc.qt.io/qt-5/signalsandslots.html).

          Qt is a asynchronous framework, so you have to think is this way: avoid using active wait or forever loops. Because this will lock the event loop and no signals could be processed!

          As Qt is asynchronous, there is no really need to use threads to handle TCP/UPD sockets. You only have to connect to according signals to be informed when there is something to be done.

          If you don't do heavy processing, you don't really need to create an additional thread to handle sockets.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @solidtyper
            Just an observation: if you're going to have thread sleep and poll every 50 milliseconds, you can presumably do same with QTimer without needing a thread for that.

            i'm very beginner

            My suggestion is if you can avoid threads, do so.

            S Offline
            S Offline
            solidtyper
            wrote on last edited by
            #12

            @JonB what i can do is to run only one thead instead of one inside another, this single thread would do everything that both of these threads do.
            but it's a pain to change all the code that prcess packets and such. that's why i asked for a method to do so instead.

            1 Reply Last reply
            0
            • KroMignonK KroMignon

              @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

              i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.

              You are looking in the wrong direction ;)
              If you want to use QObject and signals/slots, you must have a working event loop, because it is there where the signals will be processed (cf. https://doc.qt.io/qt-5/signalsandslots.html).

              Qt is a asynchronous framework, so you have to think is this way: avoid using active wait or forever loops. Because this will lock the event loop and no signals could be processed!

              As Qt is asynchronous, there is no really need to use threads to handle TCP/UPD sockets. You only have to connect to according signals to be informed when there is something to be done.

              If you don't do heavy processing, you don't really need to create an additional thread to handle sockets.

              S Offline
              S Offline
              solidtyper
              wrote on last edited by
              #13

              @KroMignon i have a thread that handles accepts cnx from listener sock and creates another one that takes care of that socket and all packets that it sends. i think this way is not going to fail when receiving many connections.

              i wanted to run only thread that listens on any sock including listener and add tasks to the queue(adding new cnx & printing msg) then another thread takes care of accepting new cnx or if it's a message it prints is out in the UI. in this way i'm gonna have two threads in parrallel instead. which is easier.

              if this doesn't work, or if it's too complicated for me that's hat i'm gonig to do.

              but skipping threads all together!! i don't think that's possible for a multi-chat server.

              JonBJ 1 Reply Last reply
              0
              • S solidtyper

                @KroMignon i have a thread that handles accepts cnx from listener sock and creates another one that takes care of that socket and all packets that it sends. i think this way is not going to fail when receiving many connections.

                i wanted to run only thread that listens on any sock including listener and add tasks to the queue(adding new cnx & printing msg) then another thread takes care of accepting new cnx or if it's a message it prints is out in the UI. in this way i'm gonna have two threads in parrallel instead. which is easier.

                if this doesn't work, or if it's too complicated for me that's hat i'm gonig to do.

                but skipping threads all together!! i don't think that's possible for a multi-chat server.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #14

                @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

                but skipping threads all together!! i don't think that's possible for a multi-chat server.

                In that case have you looked at Threaded Fortune Server Example ?

                S 1 Reply Last reply
                1
                • JonBJ JonB

                  @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

                  but skipping threads all together!! i don't think that's possible for a multi-chat server.

                  In that case have you looked at Threaded Fortune Server Example ?

                  S Offline
                  S Offline
                  solidtyper
                  wrote on last edited by
                  #15

                  @JonB i guess this will really help, since it uses one thread for all. i'll give it a try.

                  JonBJ 1 Reply Last reply
                  0
                  • S solidtyper

                    @JonB i guess this will really help, since it uses one thread for all. i'll give it a try.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #16

                    @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

                    one thread for all

                    Depends what you mean by that. One thread of each client --- like your "multi-chat server."? This example is for multi-threaded:

                    The implementation of this example is similar to that of the Fortune Server example, but here we will implement a subclass of QTcpServer that starts each connection in a different thread.

                    S 1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #17

                      @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

                      multi-chat server.

                      Hi
                      If that is the goal, I must point to
                      https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application

                      As it explains in detail the design of such app

                      1 Reply Last reply
                      1
                      • JonBJ JonB

                        @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

                        one thread for all

                        Depends what you mean by that. One thread of each client --- like your "multi-chat server."? This example is for multi-threaded:

                        The implementation of this example is similar to that of the Fortune Server example, but here we will implement a subclass of QTcpServer that starts each connection in a different thread.

                        S Offline
                        S Offline
                        solidtyper
                        wrote on last edited by solidtyper
                        #18

                        @JonB i didn't read the code carefully:
                        "QTcpServer::incomingConnection() creates a FortuneThread object, passing the incoming socket descriptor"

                        i thought it only runs one thread that loops through each socket and prints msg if there is any and accpet cnx if there is any.

                        that's what i did right now using (fd_set, _clr, etc). it runs on one thread and waits for a connection then checks if its comming from listener sock(new cnx) or an existing sock(msg), and acts accordingly.

                        however i find it not very efficient.

                        what i think is a better solution is two threads in parallel: one listens for any cnx(new cnx/msg) and add tasks to a queue then the second one continuesly checks for tasks in the queue and do what it says.

                        i don't know if this also going to cause the same issue as before with nested threads in case i needed to communicate between them though.

                        KroMignonK 1 Reply Last reply
                        0
                        • S solidtyper

                          @JonB i didn't read the code carefully:
                          "QTcpServer::incomingConnection() creates a FortuneThread object, passing the incoming socket descriptor"

                          i thought it only runs one thread that loops through each socket and prints msg if there is any and accpet cnx if there is any.

                          that's what i did right now using (fd_set, _clr, etc). it runs on one thread and waits for a connection then checks if its comming from listener sock(new cnx) or an existing sock(msg), and acts accordingly.

                          however i find it not very efficient.

                          what i think is a better solution is two threads in parallel: one listens for any cnx(new cnx/msg) and add tasks to a queue then the second one continuesly checks for tasks in the queue and do what it says.

                          i don't know if this also going to cause the same issue as before with nested threads in case i needed to communicate between them though.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #19

                          @solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):

                          however i find it not very efficient.

                          How many sockets do you want to handle?
                          Are you really sure that using multiple thread will increase application performances?

                          If you want to do multi-threading, please take this hints in account:

                          • avoid creating too many threads, the best is not to create more than your CPU has core (cf QThread::idealThreadCount())
                          • override QTcpServer::incomingConnection() to be able to create a QTcpSocket instance and move it in the working thread (as descibred in https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application)

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          2

                          • Login

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