Background tasks: Resource management and cancellation


This talk is about the analysis of one problem that is often encountered in practice — the removal of a heavy operation from a UI thread. Of course, the problem is not new or even unique to C ++. But unlike languages, garbage collection in C++ requires a nontrivial effort to ensure that the resources used by the background task live long enough. The obvious solution is to use shared_ptr everywhere, but sometimes you need something subtler (and more efficient).

Another nontrivial part of working with background operations is the implementation of cancellation. And of course, since we are talking about C++, both working with resources and support for cancellation should be as efficient as possible, with a minimum of allocations and mutexes.

We will take a look at how it all can be written one by one, with three main mechanisms of asynchronous programming: callbacks, (self-written) futures, and C++ 20 coroutines, which are each of the tradeoffs approaches. At the end of the talk, we'll touch on the fashionable concept of Structured Concurrency.