olegabr
olegabr

Reputation: 545

How to properly wait for the async http request with no "Connection: close" header to complete in the cpp-netlib?

I'm opening a long lived connection with a remote server. Server send me updates in this connection for a long time. So, I do not use the Connection: close header as suggested in cpp-netlib docs in my request. And I use a streaming body handler to process server data: http://cpp-netlib.org/0.12.0/reference/http_client.html#streaming-body-handler.

The http connection lifetime thread does the join call in the http client object destructor:

When a client object is destroyed, it waits for all pending asynchronous operations to finish.

I have a problem with this behaviour when my streaming body handler accesses data that already have been destroyed.

I have a workaround to explicitly call a http client destructor to perform the thread.join when it is safe for me:

class MyClient {
    std::shared_ptr<http::client> client_;
    std::string this_member_would_be_destroyed_before_the__client_;
};

int main() {
    MyClient client;
    client.join();
}

void MyClient::join() {
    client_.reset(new http::client);
}

Doing this way I avoid the MyClient object destructor call and all it's members stay live.

The question: is it the right way to accomplish that task, or is there a better solution?

Upvotes: 1

Views: 328

Answers (0)

Related Questions