gracefully shut down secondary threads

vertex-deduplication
EmaMaker 2023-03-07 21:37:28 +01:00
parent 716056b8c5
commit e69c58abd3
3 changed files with 29 additions and 30 deletions

View File

@ -7,12 +7,17 @@ namespace chunkmanager
{ {
std::thread initGenThread(); std::thread initGenThread();
std::thread initMeshThread(); std::thread initMeshThread();
void stopGenThread();
void stopMeshThread();
void update(float deltaTime); void update(float deltaTime);
void updateChunk(uint32_t, uint16_t, uint16_t, uint16_t); void updateChunk(uint32_t, uint16_t, uint16_t, uint16_t);
void destroy(); void destroy();
void mesh(); void mesh();
void generate(); void generate();
} }
#endif #endif

View File

@ -8,6 +8,7 @@
#include "chunkmesher.hpp" #include "chunkmesher.hpp"
#include "globals.hpp" #include "globals.hpp"
#include <atomic>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <set> #include <set>
@ -27,10 +28,12 @@ namespace chunkmanager
std::set<Chunk::Chunk *> to_mesh; std::set<Chunk::Chunk *> to_mesh;
std::set<Chunk::Chunk *> to_mesh_shared; std::set<Chunk::Chunk *> to_mesh_shared;
std::atomic_bool mesh_should_run;
std::atomic_bool generate_should_run;
void mesh() void mesh()
{ {
while (true) while (mesh_should_run)
{
if (mutex_queue_mesh.try_lock()) if (mutex_queue_mesh.try_lock())
{ {
for (const auto &c : to_mesh) for (const auto &c : to_mesh)
@ -45,33 +48,11 @@ namespace chunkmanager
to_mesh.clear(); to_mesh.clear();
mutex_queue_mesh.unlock(); mutex_queue_mesh.unlock();
} }
// while (!to_mesh.empty())
// {
// Chunk::Chunk *c = to_mesh.front();
// if (c->mutex_state.try_lock())
// {
// chunkmesher::mesh(c);
// // std::cout << "Meshing chunk at " << c->getPosition().x << ", " << c->getPosition().y << ", " << c->getPosition().z << "\n";
// c->setState(Chunk::CHUNK_STATE_MESHED, true);
// c->mutex_state.unlock();
// to_mesh.pop();
// }
// }
// if(mutex_queue_mesh.try_lock()){
// to_mesh = to_mesh_shared;
// mutex_queue_mesh.unlock();
// }
// std::cout << "To mesh empty\n";
}
} }
void generate() void generate()
{ {
while (true) while (generate_should_run)
{
if (mutex_queue_generate.try_lock()) if (mutex_queue_generate.try_lock())
{ {
for (const auto &c : to_generate) for (const auto &c : to_generate)
@ -87,19 +68,28 @@ namespace chunkmanager
mutex_queue_generate.unlock(); mutex_queue_generate.unlock();
} }
} }
}
std::thread initMeshThread() std::thread initMeshThread()
{ {
mesh_should_run = true;
std::thread mesh_thread(mesh); std::thread mesh_thread(mesh);
return mesh_thread; return mesh_thread;
} }
std::thread initGenThread() std::thread initGenThread()
{ {
generate_should_run = true;
std::thread gen_thread(generate); std::thread gen_thread(generate);
return gen_thread; return gen_thread;
} }
void stopGenThread(){
generate_should_run = false;
}
void stopMeshThread(){
mesh_should_run = false;
}
int total{0}, toGpu{0}; int total{0}, toGpu{0};
int rr{RENDER_DISTANCE * RENDER_DISTANCE}; int rr{RENDER_DISTANCE * RENDER_DISTANCE};
uint8_t f = 0; uint8_t f = 0;

View File

@ -1,4 +1,3 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -94,12 +93,17 @@ int main()
glfwPollEvents(); glfwPollEvents();
} }
delete theShader; chunkmanager::stopGenThread();
chunkmanager::stopMeshThread();
genThread.join(); genThread.join();
meshThread.join(); meshThread.join();
chunkmanager::destroy(); chunkmanager::destroy();
delete theShader;
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }