From 23222f01de3f2be688ce98c8e6e8fea464da24f3 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Thu, 17 Sep 2020 09:43:24 +0800 Subject: [PATCH] add threaded client example --- README.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bfc5e1..0fd5a13 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Git repo: https://github.com/sprinfall/webcc. Please check this one instead of t * [POST Request](#post-request) * [Downloading Files](#downloading-files) * [Uploading Files](#uploading-files) + * [Client API and Threads](#client-api-and-threads) * [Server API](#server-api) * [A Minimal Server](#a-minimal-server) * [URL Route](#url-route) @@ -207,7 +208,36 @@ The file will not be loaded into the memory all at once, instead, it will be rea Please note that `Content-Length` header will still be set to the true size of the file, this is different from the handling of chunked data (`Transfer-Encoding: chunked`). -Please check the [examples](examples/) for more information. +### Client API and Threads + +A `ClientSession` shouldn't be shared by multiple threads. Please create a new session for each thread. + +Example: + +```cpp +void ThreadedClient() { + std::vector threads; + + for (int i = 0; i < 3; ++i) { + threads.emplace_back([]() { + webcc::ClientSession session; + + try { + auto r = session.Send(webcc::RequestBuilder{}. + Get("http://httpbin.org/get") + ()); + std::cout << r->data() << std::endl; + + } catch (const webcc::Error&) { + } + }); + } + + for (auto& t : threads) { + t.join(); + } +} +``` ## Server API