From 34292dd547fcd88bccd4aee16f702ca4778393a5 Mon Sep 17 00:00:00 2001 From: baldurk Date: Sun, 24 Apr 2016 13:14:48 +0200 Subject: [PATCH] Fix race condition where 'visible' result is checked before job finishes --- multithreading/multithreading.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/multithreading/multithreading.cpp b/multithreading/multithreading.cpp index f955fb1a..b5507d84 100644 --- a/multithreading/multithreading.cpp +++ b/multithreading/multithreading.cpp @@ -432,21 +432,28 @@ public: updateSecondaryCommandBuffer(inheritanceInfo); commandBuffers.push_back(secondaryCommandBuffer); + // Add a job to the thread's queue for each object to be rendered for (uint32_t t = 0; t < numThreads; t++) { - // Add a job to the thread's queue for each object to be rendered for (uint32_t i = 0; i < numObjectsPerThread; i++) { threadPool.threads[t]->addJob([=] { threadRenderCode(t, i, inheritanceInfo); }); - // Only submit if object is within the current view frustum + } + } + + threadPool.wait(); + + // Only submit if object is within the current view frustum + for (uint32_t t = 0; t < numThreads; t++) + { + for (uint32_t i = 0; i < numObjectsPerThread; i++) + { if (threadData[t].objectData[i].visible) { commandBuffers.push_back(threadData[t].commandBuffer[i]); } } } - - threadPool.wait(); // Execute render commands from the secondary command buffer vkCmdExecuteCommands(primaryCommandBuffer, commandBuffers.size(), commandBuffers.data());