Skip to content

Commit

Permalink
Example to disable std::this_thread::sleep_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Oct 29, 2024
1 parent 5c1a34e commit 0917975
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
24 changes: 19 additions & 5 deletions example/ssecli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@
using namespace std;

int main(void) {
httplib::Client("http://localhost:1234")
.Get("/event1", [&](const char *data, size_t data_length) {
std::cout << string(data, data_length);
return true;
});
std::vector<std::thread> threads;

size_t threead_count = 64;

for (size_t i = 0; i < threead_count; i++) {
threads.push_back(std::thread{[&, i] {
httplib::Client("http://localhost:1234")
.Get("/event1", [&, i](const char *data, size_t data_length) {
if (i == threead_count - 1) {
std::cout << string(data, data_length);
}
return true;
});
}});
}

for (auto &t : threads) {
t.join();
}

return 0;
}
7 changes: 5 additions & 2 deletions example/ssesvr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ using namespace std;

class EventDispatcher {
public:
EventDispatcher() {
}
EventDispatcher() {}

void wait_event(DataSink *sink) {
unique_lock<mutex> lk(m_);
Expand Down Expand Up @@ -64,6 +63,10 @@ int main(void) {

Server svr;

svr.new_task_queue = [] {
return new ThreadPool(/*num_threads=*/128, /*max_queued_requests=*/256);
};

svr.Get("/", [&](const Request & /*req*/, Response &res) {
res.set_content(html, "text/html");
});
Expand Down
2 changes: 1 addition & 1 deletion httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3283,7 +3283,7 @@ inline bool keep_alive(const std::atomic<socket_t> &svr_sock, socket_t sock,
return true; // Ready for read
}

std::this_thread::sleep_for(microseconds{interval_usec});
// std::this_thread::sleep_for(microseconds{interval_usec});
}

return false;
Expand Down

0 comments on commit 0917975

Please sign in to comment.