File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change 22
33#include " ChatbotAMD.h"
44
5+ #include < algorithm>
56#include < chrono>
67#include < future>
78#include < iostream>
@@ -33,6 +34,7 @@ ServerAMD::Chatbot::greetAsync(
3334
3435 // Simulate a long-running background operation by using std::async.
3536 // Note that we're moving all arguments except current into the lambda expression.
37+
3638 _tasks.push_back (std::async (
3739 std::launch::async,
3840 [name = std::move (name), response = std::move (response), exception = std::move (exception)]()
@@ -43,6 +45,15 @@ ServerAMD::Chatbot::greetAsync(
4345 response (os.str ());
4446 }));
4547
48+ // We don't want the _tasks vector to grow forever so remove all completed tasks here, without waiting.
49+ // TODO: switch to std::erase_if when we can use C++20.
50+ _tasks.erase (
51+ std::remove_if (
52+ _tasks.begin (),
53+ _tasks.end (),
54+ [](const auto & task) { return task.wait_for (std::chrono::seconds (0 )) == std::future_status::ready; }),
55+ _tasks.end ());
56+
4657 // greetAsync completes immediately (releasing the Ice server thread pool thread) while the task runs in the
4758 // background.
4859}
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ namespace ServerAMD
1111 class Chatbot : public VisitorCenter ::Greeter
1212 {
1313 public:
14- // Waits for all tasks to complete
14+ // Waits for all outstanding tasks to complete.
1515 ~Chatbot () override ;
1616
1717 // Implements the pure virtual function in the base class (VisitorCenter::Greeter) generated by the Slice
You can’t perform that action at this time.
0 commit comments