I was working on some code where the source buffer for updating a texture was shared from another thread and of a different frame rate than the GUI thread. It looks something like:
// GUI Thread (handles menus, events, and display)
while(window.isOpen())
{
// Rest of GUI and event logic
if(atomicOutputPixelsUpdated)
{
sharedData.outputPixelsMutex.lock();
atomicOutputPixelsUpdated = false;
// Copy pixel data into outputTexture
sharedData.outputPixelsMutex.unlock();
}
outputTexture.update(&texturePixels[0]);
window.draw(outputSprite);
window.display();
}
Mutexes, atomics, and threads are the builtins provided by the std lib (<mutex>, <atomic>, and <thread>) rather than SFML. In a separate thread there is code along the lines of:
// Do a bunch of logic
sharedData.outputPixelsMutex.lock();
// Copy the results into the shared buffer
atomicOutputPixelsUpdated = true;
sharedData.outputPixelsMutex.unlock();
// Naively rate limit the task so we don't eat up the core
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Note that in each case the mutex only locks at the read/write pixels portion of the logic, not the entire function.
For some reason if I run this with setVerticalSync enabled the logic thread will only update 60 times per second. If I use this with setFrameRate(60) then the logic updates ~1000 times per second. Why is this? I would figure since the mutex is unlocked before draw() is called it shouldn't be blocked by the vsync wait? Is there any way I can have asynchronus data fed into my draw thread without causing the other thread to run in lockstep?