thread stop clean up
This commit is contained in:
14
spawner.h
14
spawner.h
@@ -11,11 +11,10 @@ template<typename T, size_t N>
|
||||
struct Spawner {
|
||||
std::mutex mutex;
|
||||
std::condition_variable cv;
|
||||
std::atomic<bool> running;
|
||||
std::atomic<bool> paused;
|
||||
std::jthread timer;
|
||||
long long rate_secs;
|
||||
T values[N];
|
||||
std::atomic<unsigned long long> rate_secs;
|
||||
std::array<T, N> values;
|
||||
|
||||
explicit Spawner(long long rate_secs);
|
||||
|
||||
@@ -44,9 +43,8 @@ void Spawner<T, N>::spawn() {
|
||||
|
||||
template<typename T, size_t N>
|
||||
void Spawner<T, N>::start() {
|
||||
running.store(true);
|
||||
timer = std::jthread([&] {
|
||||
while (running.load()) {
|
||||
timer = std::jthread([&] (const std::stop_token &st) {
|
||||
while (!st.stop_requested()) {
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
|
||||
std::unique_lock lock{mutex};
|
||||
@@ -55,14 +53,14 @@ void Spawner<T, N>::start() {
|
||||
}
|
||||
|
||||
auto next = start + std::chrono::seconds(rate_secs);
|
||||
cv.wait_until(lock, next, [&] { return !running.load(); });
|
||||
cv.wait_until(lock, next);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
Spawner<T, N>::~Spawner() {
|
||||
running.store(false);
|
||||
timer.request_stop();
|
||||
cv.notify_all();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user