Thread with no event loop, detect an exit event
-
Hi all, I write a program which has a separate thread with computation routines about 1..10..30 minutes long. So event loop is useless here. But I'd like to keep thread manageable so I'm wondering how working slot should query thread instance about posted termination event. Are there any best practices to manage threads with really huge computations?
-
You could use a variable which is set (lets say to true) when you want to terminate the thread. The thread has to check that variable periodically and terminate if it is set.
-
@Northsoft said:
But I'd like to keep thread manageable so I'm wondering how working slot should query thread instance about posted termination event.
If termination is the only thing you care I'd pass a const pointer to std::atomic_bool that the computation routine checks and terminates if false.
If you can use C++11, for this kind of things where you don't need an event loop, I'd use the standard std::async instead as it might be more efficient and reduce the overhead of QThread
-
@Northsoft
Hello,http://doc.qt.io/qt-5/qthread.html#isInterruptionRequested
http://doc.qt.io/qt-5/qthread.html#requestInterruptionI don't think it's reasonable to use an event loop, without using an event loop (what you asked). You either use the event loop and batch your calculation work through that, or you don't and you handle the thread control by hand.
Kind regards.