diff --git a/example/ssecli.cc b/example/ssecli.cc index 2c938229f6..be1b654e4e 100644 --- a/example/ssecli.cc +++ b/example/ssecli.cc @@ -11,11 +11,28 @@ 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; - }); + vector threads; + map counts; + + size_t thread_count = 20; + + for (size_t i = 0; i < thread_count; i++) { + threads.push_back(thread{[&] { + httplib::Client("http://localhost:1234") + .Get("/event1", [&](const char *data, size_t data_length) { + auto d = string(data, data_length - 2); + counts[d]++; + if (counts[d] == thread_count) { + cout << d << endl; + } + return true; + }); + }}); + } + + for (auto &t : threads) { + t.join(); + } return 0; } diff --git a/example/ssesvr.cc b/example/ssesvr.cc index 5b0e0b93bf..220b74ac92 100644 --- a/example/ssesvr.cc +++ b/example/ssesvr.cc @@ -12,8 +12,7 @@ using namespace std; class EventDispatcher { public: - EventDispatcher() { - } + EventDispatcher() {} void wait_event(DataSink *sink) { unique_lock lk(m_); @@ -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"); }); @@ -90,8 +93,8 @@ int main(void) { int id = 0; while (true) { this_thread::sleep_for(chrono::seconds(1)); - cout << "send event: " << id << std::endl; - std::stringstream ss; + cout << "send event: " << id << endl; + stringstream ss; ss << "data: " << id << "\n\n"; ed.send_event(ss.str()); id++;