From ca043bac68f6fa4bf797108ff970e191d4a7ea2a Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Tue, 3 Oct 2023 22:01:32 +0200 Subject: [PATCH] threads: allow for proper shutdown using `if` instead of `while` avoids the need to wait for the queue to empty to shutdown the thread --- src/chunkmanager.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 97b0adf..1d5758b 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -95,8 +95,9 @@ namespace chunkmanager void generate(){ while(should_run){ ChunkPQEntry entry; - while(chunks_to_generate_queue.try_pop(entry)) generateChunk(entry.first); + if(chunks_to_generate_queue.try_pop(entry)) generateChunk(entry.first); } + chunks_to_generate_queue.clear(); } // Method for chunk meshing thread(s) @@ -111,6 +112,7 @@ namespace chunkmanager } } } + chunks_to_mesh_queue.clear(); } oneapi::tbb::concurrent_queue chunks_todelete; @@ -185,10 +187,16 @@ namespace chunkmanager void stop() { should_run=false; + + std::cout << "Waiting for secondary threads to shut down" << std::endl; update_thread.join(); + std::cout << "Update thread has terminated" << std::endl; gen_thread.join(); + std::cout << "Generation thread has terminated" << std::endl; mesh_thread.join(); + std::cout << "Meshing thread has terminated" << std::endl; } + void destroy(){ /*for(const auto& n : chunks){ delete n.second; @@ -314,3 +322,4 @@ namespace chunkmanager } } }; +