Solved How can i start a thread after the end of another thread
-
how I can start a thread after the end of another thread
I found this statement:
future.waitForFinished ();
and I put it in the second thread but the problem is what do I have to add to :
QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread -
@jagl
I haven't usedQFuture
s, but (a) shouldn't yourwaitForFinished()
be for the first thread you set off, not the second, and (b) shouldn't you onlyrun()
the second thread once the first thread has hitwaitForFinished()
, not before that? -
@jagl said in How can i start a thread after the end of another thread:
and I put it in the second thread
What?
It is as simple as:QFuture<void> futureSec = QtConcurrent::run(Tab ); futureSec.waitForFinished ();
Keep in mind that this way you're blocking the thread which starts the other thread.
If you need more control and non-blocking behaviour use QThread and connect a slot to https://doc.qt.io/qt-5/qthread.html#finished signal. -
@jsulm yes that's why I put it in the function of the second thread
void Tab ( QFuture<void> future) {
future.waitForFinished();
//the rest of the code
}
void bateau::on_bat_clicked()
{
QFuture<void> future = QtConcurrent::run(Cap);
QFuture<void> futureSec = QtConcurrent::run(Tab, ??? );//this is the second thread
} -
@jagl
And how did you change this in response to my earlier reply, telling you what you need to do? -
QFuture<void> futureSec = QtConcurrent::run(Tab, future);
-
@JonB I just want to start the second thread after the end of the first one
-
@jagl said in How can i start a thread after the end of another thread:
how I can start a thread after the end of another thread
I found this statement:
future.waitForFinished ();
and I put it in the second thread but the problem is what do I have to add to :
QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second threadI have some difficulties to understand your problem.
I supposeTab
is a function you want to start in a dedicated thread.
So if you only want to be aware aboutQtConcurrent::run()
, why not simply using aQFutureWatcher
?auto watcher = new QFutureWatcher<void>(); connect(watcher, &QFutureWatcher<void>::finished, [watcher, this]() { // do your stuff ... //cleanup watcher->deleteLater(); }); watcher->setFuture(QtConcurrent::run(Tab));
-
@KroMignon thanks
-
@KroMignon's answer is perfectly fine. However, I was thinking if it couldn't be done any easier.
Couldn't we drop
QFuture
/QFutureWatcher
altogether? How about:QtConcurrent::run([this]() { Cap(); QtConcurrent::run(Tab); });
The only difference is from which thread the second
QtConcurrent::run
is started. Wouldn't this make it a lot easier? (DoesQtConcurrent::run
also take a lambda?) -
@SimonSchroeder said in How can i start a thread after the end of another thread:
The only difference is from which thread the second QtConcurrent::run is started. Wouldn't this make it a lot easier?
Why not, it always depends on the use case.
(Does QtConcurrent::run also take a lambda?)
For such question, always check documentation ;) ==> https://doc.qt.io/qt-5/qtconcurrentrun.html#using-lambda-functions