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. Qthread and movetothread
Forum Updated to NodeBB v4.3 + New Features

Qthread and movetothread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 12.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.
  • H Offline
    H Offline
    homi1
    wrote on last edited by
    #1

    Hello,

    below are 2 classes - Worker contains the job to be done. startThread in Starter creates the new thread and moves the doWork-Method to the new thread.

    I don't understand, why the wait call in startThread never returns, when *AutoConnection *or *QueuedConnection *is used. (in the connect-call) Only if *DirectConnection *is used, the debug message after wait appears.

    If I understand it right, DirectConnection should not be used in this example. Because doWork runs in a different thread.

    In the example qt 4.8.1 is used.

    Please, can somebody help?

    @#ifndef WORKER_H
    #define WORKER_H

    #include <QObject>
    #include <QDebug>
    #include <QThread>

    class Worker : public QObject
    {
    Q_OBJECT
    public:
    Worker();
    signals:
    void finished();
    public slots:
    void doWork()
    {
    for (int i = 0; i < 5; i++)
    {
    qDebug() << "hello world" << QThread::currentThreadId();
    }
    emit finished();
    }
    };

    #endif // WORKER_H@

    @#ifndef STARTER_H
    #define STARTER_H

    #include <QObject>
    #include <QThread>
    #include <QDebug>

    #include "worker.h"

    class Starter : public QObject
    {
    Q_OBJECT
    public:
    Starter();

    void startThread()
    {
        Worker w;
        QThread t;
    
        w.moveToThread(&t);
    
        connect(&t, SIGNAL(started()), &w, SLOT(doWork()), Qt::DirectConnection);
        connect(&w, SIGNAL(finished()), &t, SLOT(quit()), Qt::DirectConnection);
    
        t.start();
    
        qDebug() << "start";
        t.wait();
        qDebug() << "ende";
    }
    

    };

    #endif // STARTER_H@

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      What do you want to do? Do you want to have "hello world" before "start"?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • H Offline
        H Offline
        homi1
        wrote on last edited by
        #3

        no, I want an "ende" message.

        If I use Qt::QueuedConnection or Qt::AutoConnection the "ende" message is never displayed. The program waits in "t.wait" forever.

        I don't understand why.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          You are running a standard QThread and did not overwrite the run() method. That is okay. It's actually how they recommend using QThread nowadays. However you need to be aware that the default implementation of QThread::run() will run an Event Loop and thus won't exit until QThread::quit() has been called. As you never call that function, QThread::run() never terminates. It happily continues event processing and you will wait forever.

          Instead of waiting synchronously inside startThread(), you might simply connect the Worker's finished() Signal to some other Slot of your Starter class. Whatever needs to be done when the worker has finished, you could to it there. But if you absoloutely need to wait in a synchronous way, you might replace "t.wait()" with:

          @QEventLoop loop;
          connect(&w, SIGNAL(finsihed()), &loop, SLOT(quit()));
          loop.exec(QEventLoop::ExcludeUserInputEvents);@

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • I Offline
            I Offline
            INeedMySpace
            wrote on last edited by
            #5

            It's a not very good solution cause when you start doWork you can't stop it till it finished.
            You can try to modify your example by starting your work with singleshot timer after thread is started.

            @QTimer::singleShot(0, w, SLOT(doWork());@

            and remove
            @connect(&t, SIGNAL(started()), &w, SLOT(doWork()), Qt::DirectConnection);@

            Also, as I understand, you should use auto or queued connection here, cause worker and thread are in different threads.
            @connect(&w, SIGNAL(finished()), &t, SLOT(quit()));@

            You have been involved in illegal use of magic.

            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