Skip to content

Commit ea16092

Browse files
Fix ChatbotAMD to clear completed futures (#290)
1 parent 9d0cb9e commit ea16092

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

cpp/Ice/greeter/ChatbotAMD.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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
}

cpp/Ice/greeter/ChatbotAMD.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)