I have a desktop application where certain computations, when requested by the user, are run in a background thread. There is a cancel button.
I know the "safe" or "correct" ways of signaling cancellation to the background task (using Qt signal/slot connections, mutex-wrapped booleans on which the background task polls, etc.).
However, the simplest thing seems to me to be having a bool cancelled
in my main-thread class, which is set synchronously when the Cancel button is pressed, and passing a const bool &cancelled
to the background thread, on which it polls.
Is there any realistic way this approach can backfire?
The threads may run on separate processors, each with its own cache. If the boolean is atomic you can do this. Otherwise you risk that a change is only propagated as far as the nearest cache, and not visible to the other thread.
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