Skip to content

Commit 95b22a9

Browse files
committed
Fixed yhirose#35
1 parent 38bbe4e commit 95b22a9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

example/server.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <httplib.h>
99
#include <cstdio>
10+
#include <chrono>
1011

1112
#define SERVER_CERT_FILE "./cert.pem"
1213
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
@@ -77,7 +78,13 @@ int main(void)
7778
});
7879

7980
svr.get("/hi", [](const auto& /*req*/, auto& res) {
80-
res.set_content("Hello World!", "text/plain");
81+
res.set_content("Hello World!\n", "text/plain");
82+
});
83+
84+
svr.get("/slow", [](const auto& /*req*/, auto& res) {
85+
using namespace std::chrono_literals;
86+
std::this_thread::sleep_for(2s);
87+
res.set_content("Slow...\n", "text/plain");
8188
});
8289

8390
svr.get("/dump", [](const auto& req, auto& res) {

httplib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef int socket_t;
5555
#include <memory>
5656
#include <regex>
5757
#include <string>
58+
#include <thread>
5859
#include <sys/stat.h>
5960
#include <assert.h>
6061

@@ -1236,7 +1237,13 @@ inline bool Server::listen(const char* host, int port, int socket_flags)
12361237
}
12371238

12381239
// TODO: should be async
1240+
#ifdef CPPHTTPLIB_NO_MULTI_THREAD_SUPPORT
12391241
read_and_close_socket(sock);
1242+
#else
1243+
std::thread([=]() {
1244+
read_and_close_socket(sock);
1245+
}).detach();
1246+
#endif
12401247
}
12411248

12421249
return ret;

0 commit comments

Comments
 (0)