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 Nov 16, 2024
1 parent 4139949 commit afac7e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
27 changes: 22 additions & 5 deletions example/ssecli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<thread> threads;
map<string, size_t> 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;
}
11 changes: 7 additions & 4 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 All @@ -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++;
Expand Down

0 comments on commit afac7e0

Please sign in to comment.