Skip to content

Clean application shutdown #50

@deeringc

Description

@deeringc
Contributor

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

Activity

ras0219-msft

ras0219-msft commented on Jan 19, 2016

@ras0219-msft
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

glukacsy commented on Jan 24, 2016

@glukacsy

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

megaposer commented on Jan 25, 2016

@megaposer
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @deeringc@megaposer@glukacsy@ras0219-msft

        Issue actions

          Clean application shutdown · Issue #50 · microsoft/cpprestsdk