cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

Clean application shutdown

Open deeringc opened this issue 10 years ago • 3 comments

Hi folks,

I just wanted to check with you what the best practices are for cleanly shutting down an application running Casablanca with ongoing async tasks? Normally in a multi-thread application we would join all worker threads. What's the right usage pattern for Casablanca and PPLX?

Thanks, Chris

deeringc avatar Jan 07 '16 16:01 deeringc

The right pattern is pretty much the same -- you should be able to trace how all the asynchronous tasks join together back into your main thread.

If you're inside a task, this can be accomplished by returning the inner task you'd like to wait on. If you're simply in a normal c++ thread such as main(), you can call .get() to "join" a task.

ras0219-msft avatar Jan 19 '16 19:01 ras0219-msft

What does it mean in practice though? For example, what happens if we are in the middle of a network operation when we are exiting the application? Let's say we issued the request on an http_client object and the underlying network stack (and casablanca) is doing it's work, downloading a large file or waiting for a network timeout. How should we safely cancel such a task? In our app we have hundreds of these simultaneously (downloading avatars for people, tokens for decrypting contents etc). Should we keep track of all these request tasks? And if we have these tracked, how do we cancel them?

Thanks. Gergely

glukacsy avatar Jan 24 '16 19:01 glukacsy

When you create a request on an http_client object, you can provide a const reference to a pplx::cancellation_token object.

One way to utilize this is to create a pplx::cancellation_token_source and pass that into the request via source.get_token().

On shutdown of your application you can then invoke a source.cancel() method which will cancel the token provided to the request. If you have a continuation task on your long running operation (large file download, waiting for a network timeout), then you would need to catch exceptions within that task, i.e. pplx::task_canceled.

megaposer avatar Jan 25 '16 07:01 megaposer