You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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?
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.
Activity
ras0219-msft commentedon Jan 19, 2016
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 commentedon Jan 24, 2016
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 commentedon Jan 25, 2016
When you create a request on an
http_client
object, you can provide a const reference to applx::cancellation_token
object.One way to utilize this is to create a
pplx::cancellation_token_source
and pass that into the request viasource.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
.