Simplify mutex locking scheme (lock at start, unlock at end)

This commit is contained in:
2025-12-12 03:08:56 +00:00
parent 00858cf74f
commit f70517cf41
3 changed files with 5 additions and 10 deletions

View File

@@ -22,8 +22,7 @@ namespace cw::state
std::queue<u64> queue; std::queue<u64> queue;
bool pause_work, stop_work; bool pause_work, stop_work;
std::mutex mutex_queue; std::mutex mutex;
std::mutex mutex_allocator;
State(void) {}; State(void) {};
}; };

View File

@@ -35,27 +35,23 @@ namespace cw::worker
void do_iteration(State &state) void do_iteration(State &state)
{ {
state.mutex_queue.lock(); // state.mutex.lock();
if (state.queue.empty()) if (state.queue.empty())
{ {
// Unlock since there isn't any work to be done. // Unlock since there isn't any work to be done.
state.mutex_queue.unlock(); // state.mutex.unlock();
return; return;
} }
u64 index = state.queue.front(); u64 index = state.queue.front();
state.queue.pop(); state.queue.pop();
state.mutex_queue.unlock();
u64 left_child, right_child; u64 left_child, right_child;
state.mutex_allocator.lock();
std::tie(left_child, right_child) = std::tie(left_child, right_child) =
generate_children(state.allocator, index); generate_children(state.allocator, index);
state.mutex_allocator.unlock();
state.mutex_queue.lock();
state.queue.push(left_child); state.queue.push(left_child);
state.queue.push(right_child); state.queue.push(right_child);
state.mutex_queue.unlock(); // state.mutex.unlock();
} }
void worker(State &state) void worker(State &state)

View File

@@ -18,7 +18,7 @@ namespace cw::worker
using cw::node::NodeAllocator; using cw::node::NodeAllocator;
using cw::state::State; using cw::state::State;
constexpr auto THREAD_PAUSE_DELAY = std::chrono::milliseconds(1000); constexpr auto THREAD_PAUSE_DELAY = std::chrono::milliseconds(1000);
constexpr auto THREAD_GENERAL_DELAY = std::chrono::milliseconds(1); constexpr auto THREAD_GENERAL_DELAY = std::chrono::nanoseconds(100);
// Given `index`, return the indices of its children in the tree. If not // Given `index`, return the indices of its children in the tree. If not
// present already, generate them using the allocator. // present already, generate them using the allocator.