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 .
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.
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().
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.
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.
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(); };
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With