Hi,
I've a question about how to design (and implement, so I don't really know if the question would be better placed under construction) an operation cancellation mechanism.
I've an operation which runs in its own thread. It does some operations working with a remote server. The user of the operation (which could be a GUI or a CLI client) could decide to cancel the operation. Stopping the thread is not an option because the operation should gracefully handle the cancellation (finishing remote transactions, cleaning up resources and so on).
So each time I've this situation I end up with something like:
void Operation()
{
DoSomeProcessing();
if( Cancelled() )
{
CleanUpAndLeave();
}
DoSomeMoreProcessing();
if( Cancelled() )
{
CleanUpAndLeave();
}
}
Ok, the "cancellation" can be nicely implemented so that no repeated code is introduced, but still, I'm not very happy about how this looks like. The "Cancelled" method will check some variable being potentially accessed by multiple threads so it's correctly managed for multi-thread race conditions.
The point is: which one would be the right pattern to use here to make the code look cleaner?
Thanks,
pablo
Pablo is CEO at Codice Software, the company behind Plastic SCM.
www.plasticscm.com