Simplify mutex locking scheme (lock at start, unlock at end)
This commit is contained in:
@@ -22,8 +22,7 @@ namespace cw::state
|
||||
std::queue<u64> queue;
|
||||
|
||||
bool pause_work, stop_work;
|
||||
std::mutex mutex_queue;
|
||||
std::mutex mutex_allocator;
|
||||
std::mutex mutex;
|
||||
|
||||
State(void) {};
|
||||
};
|
||||
|
||||
@@ -35,27 +35,23 @@ namespace cw::worker
|
||||
|
||||
void do_iteration(State &state)
|
||||
{
|
||||
state.mutex_queue.lock();
|
||||
// state.mutex.lock();
|
||||
if (state.queue.empty())
|
||||
{
|
||||
// Unlock since there isn't any work to be done.
|
||||
state.mutex_queue.unlock();
|
||||
// state.mutex.unlock();
|
||||
return;
|
||||
}
|
||||
u64 index = state.queue.front();
|
||||
state.queue.pop();
|
||||
state.mutex_queue.unlock();
|
||||
|
||||
u64 left_child, right_child;
|
||||
state.mutex_allocator.lock();
|
||||
std::tie(left_child, right_child) =
|
||||
generate_children(state.allocator, index);
|
||||
state.mutex_allocator.unlock();
|
||||
|
||||
state.mutex_queue.lock();
|
||||
state.queue.push(left_child);
|
||||
state.queue.push(right_child);
|
||||
state.mutex_queue.unlock();
|
||||
// state.mutex.unlock();
|
||||
}
|
||||
|
||||
void worker(State &state)
|
||||
|
||||
@@ -18,7 +18,7 @@ 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);
|
||||
constexpr auto THREAD_GENERAL_DELAY = std::chrono::nanoseconds(100);
|
||||
|
||||
// Given `index`, return the indices of its children in the tree. If not
|
||||
// present already, generate them using the allocator.
|
||||
|
||||
Reference in New Issue
Block a user