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. Does moveToThread() creates a thread?

Does moveToThread() creates a thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 3.6k 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.
  • M Offline
    M Offline
    MokJ
    wrote on 9 May 2018, 13:12 last edited by
    #1

    Does moveToThread() creates a new thread ??
    A little explaination is needed.
    What it means when we say move an object to the thread?

    R 1 Reply Last reply 9 May 2018, 13:46
    0
    • G Offline
      G Offline
      Gojir4
      wrote on 9 May 2018, 13:46 last edited by
      #2

      Hello,

      No, it doesn't. You need a QThread in which you move the object.

      Here is the worker object pattern example from Qt, from QThread documentation. You can see that there is a QThread workerThread; in the class Controller, which creates the Worker object and then move it in workerThread.

      class Worker : public QObject
      {
          Q_OBJECT
      
      public slots:
          void doWork(const QString &parameter) {
              QString result;
              /* ... here is the expensive or blocking operation ... */
              emit resultReady(result);
          }
      
      signals:
          void resultReady(const QString &result);
      };
      
      class Controller : public QObject
      {
          Q_OBJECT
          QThread workerThread;
      public:
          Controller() {
              Worker *worker = new Worker;
              worker->moveToThread(&workerThread);
              connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
              connect(this, &Controller::operate, worker, &Worker::doWork);
              connect(worker, &Worker::resultReady, this, &Controller::handleResults);
              workerThread.start();
          }
          ~Controller() {
              workerThread.quit();
              workerThread.wait();
          }
      public slots:
          void handleResults(const QString &);
      signals:
          void operate(const QString &);
      };
      
      M 1 Reply Last reply 9 May 2018, 14:21
      6
      • M MokJ
        9 May 2018, 13:12

        Does moveToThread() creates a new thread ??
        A little explaination is needed.
        What it means when we say move an object to the thread?

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 9 May 2018, 13:46 last edited by
        #3

        @MokJ said in Does moveToThread() creates a thread?:

        Does moveToThread() creates a new thread ??

        no, since you already need to pass a QThread instance to moveToThread()

        What it means when we say move an object to the thread?

        Have you already read the docs?
        Why do you want to call this method? Whats your usecase?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        M 1 Reply Last reply 9 May 2018, 14:07
        6
        • R raven-worx
          9 May 2018, 13:46

          @MokJ said in Does moveToThread() creates a thread?:

          Does moveToThread() creates a new thread ??

          no, since you already need to pass a QThread instance to moveToThread()

          What it means when we say move an object to the thread?

          Have you already read the docs?
          Why do you want to call this method? Whats your usecase?

          M Offline
          M Offline
          MokJ
          wrote on 9 May 2018, 14:07 last edited by
          #4

          @raven-worx thanks for quick response
          I'm trying to build a client server chat kind of thing, and on server side I have used movertothread().
          The object I'm moving is a class in which I'm doing setSocketDescriptor() and reading and writing into the socket.
          So in void incommingConnection() I'm moving that class by moveToThread().
          Void server::incommingconnection(qintptr sd){
          Worker class* worker= new workerclass();
          Qthread *thread=new Qthread();
          Worker->moveToThread(thread);
          thread->start();

          }
          It works fine and very smooth.
          But I was thinking about efficiency, if I connect 100 clients , will my server allow 100 threads to get created?

          F 1 Reply Last reply 16 Apr 2020, 12:34
          0
          • G Gojir4
            9 May 2018, 13:46

            Hello,

            No, it doesn't. You need a QThread in which you move the object.

            Here is the worker object pattern example from Qt, from QThread documentation. You can see that there is a QThread workerThread; in the class Controller, which creates the Worker object and then move it in workerThread.

            class Worker : public QObject
            {
                Q_OBJECT
            
            public slots:
                void doWork(const QString &parameter) {
                    QString result;
                    /* ... here is the expensive or blocking operation ... */
                    emit resultReady(result);
                }
            
            signals:
                void resultReady(const QString &result);
            };
            
            class Controller : public QObject
            {
                Q_OBJECT
                QThread workerThread;
            public:
                Controller() {
                    Worker *worker = new Worker;
                    worker->moveToThread(&workerThread);
                    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
                    connect(this, &Controller::operate, worker, &Worker::doWork);
                    connect(worker, &Worker::resultReady, this, &Controller::handleResults);
                    workerThread.start();
                }
                ~Controller() {
                    workerThread.quit();
                    workerThread.wait();
                }
            public slots:
                void handleResults(const QString &);
            signals:
                void operate(const QString &);
            };
            
            M Offline
            M Offline
            MokJ
            wrote on 9 May 2018, 14:21 last edited by MokJ 5 Sept 2018, 14:24
            #5

            @Gojir4 thanks just having this doubt, please bear with me , so if I connect 100 sockets as clients to my server, how many threads are being generated?
            I'm doing QTcpSocket *socket=new QTcpSocket();
            in worker class.

            G kshegunovK 2 Replies Last reply 9 May 2018, 18:56
            0
            • M MokJ
              9 May 2018, 14:21

              @Gojir4 thanks just having this doubt, please bear with me , so if I connect 100 sockets as clients to my server, how many threads are being generated?
              I'm doing QTcpSocket *socket=new QTcpSocket();
              in worker class.

              G Offline
              G Offline
              Gojir4
              wrote on 9 May 2018, 18:56 last edited by
              #6

              @MokJ I don't think that's the best option to create a dedicated thread for every client.

              Another approach can be to run the server in a dedicated thread and use the asynchonous system which is already provided with signals-slots mechanism. For about 100 clients it could be enough, but it depends of the frequency of your requests.

              Or maybe to create a temporary thread handling each single request. Using QRunnable and QThreadPool.

              In both case, you can have a list of objects representing your clients, in the Main thread (GUI), and synchronize these objects according to your communication coming from the "Server" thread, using signal-slots.

              Anyway, that's only some suggestions, and it depends a lot of the goal of your application and the number of requests coming from clients, I'm not a specialist in this domain so it could be better to wait a reply from somebody which have more knowledge on this subjet :)

              1 Reply Last reply
              4
              • G Offline
                G Offline
                Gojir4
                wrote on 9 May 2018, 19:00 last edited by
                #7

                Did you already see this example: Network Chat Example

                1 Reply Last reply
                4
                • M MokJ
                  9 May 2018, 14:21

                  @Gojir4 thanks just having this doubt, please bear with me , so if I connect 100 sockets as clients to my server, how many threads are being generated?
                  I'm doing QTcpSocket *socket=new QTcpSocket();
                  in worker class.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 9 May 2018, 22:13 last edited by
                  #8

                  @MokJ said in Does moveToThread() creates a thread?:

                  so if I connect 100 sockets as clients to my server, how many threads are being generated?

                  That depends on your code. If you spin a thread for each client, then 100 clients means 100 threads. What one usually does when dealing with connection-heavy/high-throughput situation is to put a middleman that does the bookkeeping of which connection goes to what handler - the so called broker pattern. However it's rather complicated for the typical use case.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  3
                  • M Offline
                    M Offline
                    MokJ
                    wrote on 10 May 2018, 06:20 last edited by
                    #9

                    thankk you @Gojir4 and @kshegunov , I was thinking about putting some other design for server, I want my server to handle more clients with minimum number of threads, Whats happening now is everytime a new connection is available , server generates new thread as i have created an object of worker thread and QThread() in incommingConnection().
                    So i mean if there are any sort of suggestions , appreciated. Thanks again.

                    kshegunovK 1 Reply Last reply 10 May 2018, 22:23
                    0
                    • M MokJ
                      10 May 2018, 06:20

                      thankk you @Gojir4 and @kshegunov , I was thinking about putting some other design for server, I want my server to handle more clients with minimum number of threads, Whats happening now is everytime a new connection is available , server generates new thread as i have created an object of worker thread and QThread() in incommingConnection().
                      So i mean if there are any sort of suggestions , appreciated. Thanks again.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 10 May 2018, 22:23 last edited by
                      #10

                      Use a thread pool (basically start X number of threads beforehand) and move the objects to one of the threads in the pool "semi-randomly".

                      Read and abide by the Qt Code of Conduct

                      A 1 Reply Last reply 11 May 2018, 05:56
                      3
                      • kshegunovK kshegunov
                        10 May 2018, 22:23

                        Use a thread pool (basically start X number of threads beforehand) and move the objects to one of the threads in the pool "semi-randomly".

                        A Offline
                        A Offline
                        ambershark
                        wrote on 11 May 2018, 05:56 last edited by
                        #11

                        @kshegunov Lol someone else just basically asked this exact question here and I made the thread pool comment there. As I read this was about to do the same here and you beat me to it. Great minds think alike and all that. ;)

                        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                        1 Reply Last reply
                        2
                        • M Offline
                          M Offline
                          MokJ
                          wrote on 12 May 2018, 10:54 last edited by
                          #12

                          @kshegunov , yeah but I'm more thinking about non QThreadpool era , I mean want to do it without using QThreadpool.
                          @ambershark , I read that post you're referring to and I created a small code based on that. But I think doing it without QThreadpool can bring some more understanding about QThreads .

                          kshegunovK 1 Reply Last reply 12 May 2018, 13:09
                          0
                          • M MokJ
                            12 May 2018, 10:54

                            @kshegunov , yeah but I'm more thinking about non QThreadpool era , I mean want to do it without using QThreadpool.
                            @ambershark , I read that post you're referring to and I created a small code based on that. But I think doing it without QThreadpool can bring some more understanding about QThreads .

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on 12 May 2018, 13:09 last edited by kshegunov 5 Dec 2018, 13:11
                            #13

                            I didn't mean QThreadPool. A thread pool is a concept first, and only then it's a class.
                            Consider something like:

                            class Server : public QTcpServer
                            {
                                 Q_OBJECT 
                            
                            public:
                                 Server(QObject * = nullptr);
                                 ~Server() override;
                            
                            protected:
                                void incommingConnection(qintptr) override;
                            
                            private:
                                int counter;
                                QVector<QThread> pool;
                            
                                static int threadsCount;
                            };
                            
                            int Server::threadsCount = 5;
                            
                            Server::Server(QObject * parent)
                                : QTcpServer(parent), counter(0), pool(threadsCount)
                            {
                                for (int i = 0; i < threadsCount; i++)
                                    pool[i].start();
                            }
                            
                            Server::~Server()
                            {
                                for (int i = 0; i < threadsCount; i++)  {
                                    pool[i].quit();
                                    pool[i].wait();
                                }
                            }
                            
                            void Server::incommingConnection(qintptr descriptor)
                            {
                                Worker * worker = new Worker();
                                QThread * thread = &pool[counter++ % threadsCount];
                            
                                QObject::connect(worker, &Worker::finished, worker, &QObject::deleteLater);
                                QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater);
                            
                                worker->moveToThread(thread);
                            
                                // Initialize the socket in the correct thread
                                QMetaObject::invokeMethod(worker, [worker, descriptor] () -> void  {
                                    QTcpSocket * socket = new QTcpSocket(worker);
                                    socket->setSocketDescriptor(descriptor);
                            
                                    worker->setSocket(socket);
                                });
                            }
                            

                            Adjust the worker object as needed to make the above work.

                            Read and abide by the Qt Code of Conduct

                            M 1 Reply Last reply 13 May 2018, 12:20
                            5
                            • kshegunovK kshegunov
                              12 May 2018, 13:09

                              I didn't mean QThreadPool. A thread pool is a concept first, and only then it's a class.
                              Consider something like:

                              class Server : public QTcpServer
                              {
                                   Q_OBJECT 
                              
                              public:
                                   Server(QObject * = nullptr);
                                   ~Server() override;
                              
                              protected:
                                  void incommingConnection(qintptr) override;
                              
                              private:
                                  int counter;
                                  QVector<QThread> pool;
                              
                                  static int threadsCount;
                              };
                              
                              int Server::threadsCount = 5;
                              
                              Server::Server(QObject * parent)
                                  : QTcpServer(parent), counter(0), pool(threadsCount)
                              {
                                  for (int i = 0; i < threadsCount; i++)
                                      pool[i].start();
                              }
                              
                              Server::~Server()
                              {
                                  for (int i = 0; i < threadsCount; i++)  {
                                      pool[i].quit();
                                      pool[i].wait();
                                  }
                              }
                              
                              void Server::incommingConnection(qintptr descriptor)
                              {
                                  Worker * worker = new Worker();
                                  QThread * thread = &pool[counter++ % threadsCount];
                              
                                  QObject::connect(worker, &Worker::finished, worker, &QObject::deleteLater);
                                  QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater);
                              
                                  worker->moveToThread(thread);
                              
                                  // Initialize the socket in the correct thread
                                  QMetaObject::invokeMethod(worker, [worker, descriptor] () -> void  {
                                      QTcpSocket * socket = new QTcpSocket(worker);
                                      socket->setSocketDescriptor(descriptor);
                              
                                      worker->setSocket(socket);
                                  });
                              }
                              

                              Adjust the worker object as needed to make the above work.

                              M Offline
                              M Offline
                              MokJ
                              wrote on 13 May 2018, 12:20 last edited by
                              #14

                              @kshegunov Thanks very much , I'll try it this way

                              1 Reply Last reply
                              0
                              • M MokJ
                                9 May 2018, 14:07

                                @raven-worx thanks for quick response
                                I'm trying to build a client server chat kind of thing, and on server side I have used movertothread().
                                The object I'm moving is a class in which I'm doing setSocketDescriptor() and reading and writing into the socket.
                                So in void incommingConnection() I'm moving that class by moveToThread().
                                Void server::incommingconnection(qintptr sd){
                                Worker class* worker= new workerclass();
                                Qthread *thread=new Qthread();
                                Worker->moveToThread(thread);
                                thread->start();

                                }
                                It works fine and very smooth.
                                But I was thinking about efficiency, if I connect 100 clients , will my server allow 100 threads to get created?

                                F Offline
                                F Offline
                                FirMoon
                                wrote on 16 Apr 2020, 12:34 last edited by
                                #15

                                @MokJ No, you use threadpool instead.

                                kshegunovK 1 Reply Last reply 16 Apr 2020, 13:03
                                0
                                • F FirMoon
                                  16 Apr 2020, 12:34

                                  @MokJ No, you use threadpool instead.

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on 16 Apr 2020, 13:03 last edited by
                                  #16

                                  @FirMoon said in Does moveToThread() creates a thread?:

                                  No, you use threadpool instead.

                                  If you mean QThreadPool, no you don't! Sockets (QAbstractSocket) can be created and used only in one single thread, which is granularity which QThreadPool doesn't allow. Moreover once the socket object is created it can't be moved to another thread (directly). QThreadPool operates on atomic work units (QRunnable) and there's no guarantee in which thread from the pool the unit is going to be executed in.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  4

                                  • Login

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