Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean application shutdown #50

Open
deeringc opened this issue Jan 7, 2016 · 3 comments
Open

Clean application shutdown #50

deeringc opened this issue Jan 7, 2016 · 3 comments
Labels

Comments

@deeringc
Copy link
Contributor

deeringc commented Jan 7, 2016

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

@ras0219-msft
Copy link
Contributor

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.

@glukacsy
Copy link

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

@megaposer
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants