Skip to content

Commit afac7e0

Browse files
committed
Example to disable std::this_thread::sleep_for.
1 parent 4139949 commit afac7e0

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

example/ssecli.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,28 @@
1111
using namespace std;
1212

1313
int main(void) {
14-
httplib::Client("http://localhost:1234")
15-
.Get("/event1", [&](const char *data, size_t data_length) {
16-
std::cout << string(data, data_length);
17-
return true;
18-
});
14+
vector<thread> threads;
15+
map<string, size_t> counts;
16+
17+
size_t thread_count = 20;
18+
19+
for (size_t i = 0; i < thread_count; i++) {
20+
threads.push_back(thread{[&] {
21+
httplib::Client("http://localhost:1234")
22+
.Get("/event1", [&](const char *data, size_t data_length) {
23+
auto d = string(data, data_length - 2);
24+
counts[d]++;
25+
if (counts[d] == thread_count) {
26+
cout << d << endl;
27+
}
28+
return true;
29+
});
30+
}});
31+
}
32+
33+
for (auto &t : threads) {
34+
t.join();
35+
}
1936

2037
return 0;
2138
}

example/ssesvr.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ using namespace std;
1212

1313
class EventDispatcher {
1414
public:
15-
EventDispatcher() {
16-
}
15+
EventDispatcher() {}
1716

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

6564
Server svr;
6665

66+
svr.new_task_queue = [] {
67+
return new ThreadPool(/*num_threads=*/128, /*max_queued_requests=*/256);
68+
};
69+
6770
svr.Get("/", [&](const Request & /*req*/, Response &res) {
6871
res.set_content(html, "text/html");
6972
});
@@ -90,8 +93,8 @@ int main(void) {
9093
int id = 0;
9194
while (true) {
9295
this_thread::sleep_for(chrono::seconds(1));
93-
cout << "send event: " << id << std::endl;
94-
std::stringstream ss;
96+
cout << "send event: " << id << endl;
97+
stringstream ss;
9598
ss << "data: " << id << "\n\n";
9699
ed.send_event(ss.str());
97100
id++;

0 commit comments

Comments
 (0)