Define worker function which a thread should run
Pauses until state.pause_work is false (checking every THREAD_PAUSE_DELAY), then performs an iteration. Quits when state.quit_work is true (based on loop). Generally delays itself by THREAD_GENERAL_DELAY.
This commit is contained in:
@@ -5,9 +5,12 @@
|
||||
* Commentary:
|
||||
*/
|
||||
|
||||
#include "worker.hpp"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <tuple>
|
||||
|
||||
#include "worker.hpp"
|
||||
|
||||
namespace cw::worker
|
||||
{
|
||||
using cw::node::Fraction;
|
||||
@@ -54,6 +57,20 @@ namespace cw::worker
|
||||
state.queue.push(right_child);
|
||||
state.mutex_queue.unlock();
|
||||
}
|
||||
|
||||
void worker(State &state)
|
||||
{
|
||||
while (!state.stop_work)
|
||||
{
|
||||
std::this_thread::sleep_for(THREAD_GENERAL_DELAY);
|
||||
while (state.pause_work)
|
||||
{
|
||||
std::this_thread::sleep_for(THREAD_PAUSE_DELAY);
|
||||
}
|
||||
|
||||
do_iteration(state);
|
||||
}
|
||||
}
|
||||
} // namespace cw::worker
|
||||
|
||||
/* Copyright (C) 2025 Aryadev Chavali
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#ifndef WORKER_HPP
|
||||
#define WORKER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <tuple>
|
||||
|
||||
#include "state.hpp"
|
||||
@@ -16,6 +17,8 @@ namespace cw::worker
|
||||
{
|
||||
using cw::node::NodeAllocator;
|
||||
using cw::state::State;
|
||||
constexpr auto THREAD_PAUSE_DELAY = std::chrono::milliseconds(1000);
|
||||
constexpr auto THREAD_GENERAL_DELAY = std::chrono::milliseconds(1);
|
||||
|
||||
// Given `index`, return the indices of its children in the tree. If not
|
||||
// present already, generate them using the allocator.
|
||||
@@ -28,6 +31,10 @@ namespace cw::worker
|
||||
// Each step will block on the relevant mutex for the resource (1,3 will block
|
||||
// on the queue mutex, 2 will block on the allocator mutex) so is thread safe.
|
||||
void do_iteration(State &state);
|
||||
|
||||
// Steady living thread worker which performs iterations. If state.pause_work
|
||||
// is true, thread will pause until otherwise.
|
||||
void worker(State &state);
|
||||
} // namespace cw::worker
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user