Skip to content

Commit

Permalink
Simplify thread class
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Claussen <cc@pionix.de>
  • Loading branch information
corneliusclaussen committed Oct 11, 2023
1 parent eeeada9 commit 6e914cb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
5 changes: 1 addition & 4 deletions include/utils/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Everest {
class Thread {
public:
Thread();
~Thread();

void stop();
Expand All @@ -20,9 +19,7 @@ class Thread {

private:
std::thread handle;
std::promise<void> exitSignal;
std::future<void> exitFuture;
std::atomic_bool started;
std::atomic_bool exit_signal{false};
};
} // namespace Everest

Expand Down
21 changes: 6 additions & 15 deletions lib/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,26 @@
#include <utils/thread.hpp>

namespace Everest {
Thread::Thread() {
exitFuture = exitSignal.get_future();
started = false;
}

Thread::~Thread() {
stop();
}

void Thread::stop() {
if (started) {
exitSignal.set_value();
if (handle.joinable())
handle.join();
started = false;
if (handle.joinable()) {
exit_signal = true;
handle.join();
}
exit_signal = false;
}

bool Thread::shouldExit() {
if (exitFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
started = false;
return true;
}
return false;
return exit_signal;
}

void Thread::operator=(std::thread&& t) {
stop();
handle = std::move(t);
started = true;
}

} // namespace Everest

0 comments on commit 6e914cb

Please sign in to comment.