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.
@jsulm That's ok if nobody uses QThread::exit() method. Even if I make a slot for exit event, it could be unreachable because I don't launch event loop. Maybe there is a way to detect posted event?
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
I 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.
@kshegunov thanks a lot!