Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt thread with movetothread

Tags:

I'm trying to create a program using threads: the main start with a loop. When a test returns true, I create an object and I want that object to work in an other thread then return and start the test .

QCoreApplication a(argc, argv); while(true){     Cmd cmd;     cmd =db->select(cmd);     if(cmd.isNull()){          sleep(2);           continue ;     }     QThread *thread = new QThread( );     process *class= new process ();     class->moveToThread(thread);     thread->start();      qDebug() << " msg"; // this doesn't run until  class finish it's work  } return a.exec(); 

the problem is when i start the new thread the main thread stops and wait for the new thread's finish .

like image 210
unfamous Avatar asked Jun 14 '12 13:06

unfamous


People also ask

What is moveToThread?

moveToThread() is used to control the object's thread affinity, which basically means setting the thread (or better the Qt event loop) from which the object will emit signals and its slots will be executed.

What is QThread in Qt?

A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread. You can use worker objects by moving them to the thread using QObject::moveToThread().

How do you use Qt Threads?

To use it, prepare a QObject subclass with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That's all.

What are Q threads?

The Qthreads Library. The qthreads API is designed to make using large numbers of lightweight threads convenient and easy, and to allow portable access to threading constructs used in massively parallel shared memory environments.


2 Answers

The canonical Qt way would look like this:

 QThread* thread = new QThread( );  Task* task = new Task();   // move the task object to the thread BEFORE connecting any signal/slots  task->moveToThread(thread);   connect(thread, SIGNAL(started()), task, SLOT(doWork()));  connect(task, SIGNAL(workFinished()), thread, SLOT(quit()));   // automatically delete thread and task object when work is done:  connect(task, SIGNAL(workFinished()), task, SLOT(deleteLater()));  connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));   thread->start(); 

in case you arent familiar with signals/slots, the Task class would look something like this:

class Task : public QObject { Q_OBJECT public:     Task();     ~Task(); public slots:     // doWork must emit workFinished when it is done.     void doWork(); signals:     void workFinished(); }; 
like image 171
smerlin Avatar answered Oct 13 '22 08:10

smerlin


I don't know how you structured your process class, but this is not really the way that moveToThread works. The moveToThread function tells QT that any slots need to be executed in the new thread rather than in the thread they were signaled from. (edit: Actually, I now remember it defaults to the tread the object was created in)

Also, if you do the work in your process class from the constructor it will not run in the new thread either.

The simplest way to have your process class execute in a new thread is to derive it from QThread and override the run method. Then you never need to call move to thread at all.

like image 37
jlunavtgrad Avatar answered Oct 13 '22 09:10

jlunavtgrad