Skip to content

Commit cf3a49f

Browse files
committed
replace threads with single thread
1 parent 0e2bb78 commit cf3a49f

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/viam/sdk/robot/client.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,9 @@ RobotClient::~RobotClient() {
145145
void RobotClient::close() {
146146
should_refresh_.store(false);
147147

148-
for (auto& thread : threads_) {
149-
thread.join();
148+
if (refresh_thread_.joinable()) {
149+
refresh_thread_.join();
150150
}
151-
threads_.clear();
152151

153152
stop_all();
154153

@@ -223,7 +222,7 @@ void RobotClient::refresh() {
223222
void RobotClient::refresh_every() {
224223
while (should_refresh_.load()) {
225224
try {
226-
std::this_thread::sleep_for(std::chrono::seconds(refresh_interval_));
225+
std::this_thread::sleep_for(refresh_interval_);
227226
refresh();
228227

229228
} catch (std::exception&) {
@@ -274,10 +273,10 @@ void RobotClient::log(const std::string& name,
274273
std::shared_ptr<RobotClient> RobotClient::with_channel(ViamChannel channel,
275274
const Options& options) {
276275
auto robot = std::make_shared<RobotClient>(std::move(channel));
277-
robot->refresh_interval_ = options.refresh_interval();
278-
robot->should_refresh_ = (robot->refresh_interval_ > 0);
276+
robot->refresh_interval_ = std::chrono::seconds{options.refresh_interval()};
277+
robot->should_refresh_ = (robot->refresh_interval_ > std::chrono::seconds{0});
279278
if (robot->should_refresh_) {
280-
robot->threads_.emplace_back(&RobotClient::refresh_every, robot.get());
279+
robot->refresh_thread_ = std::thread{&RobotClient::refresh_every, robot.get()};
281280
}
282281

283282
robot->refresh();

src/viam/sdk/robot/client.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ class RobotClient {
185185

186186
void refresh_every();
187187

188-
std::vector<std::thread> threads_;
189-
188+
std::thread refresh_thread_;
190189
std::atomic<bool> should_refresh_;
191-
unsigned int refresh_interval_;
190+
std::chrono::seconds refresh_interval_;
192191

193192
ViamChannel viam_channel_;
194193

0 commit comments

Comments
 (0)