这是我正在谈论的代码的评论部分。这段代码可能看起来有点无意义,但它更复杂,我一直在简化它以找到这个问题:
void tRender(Renderer* renderer, Window* win) {
glfwSwapInterval(1);
while(true) {
renderer->isBusy = true;
renderer->draw(win); /* draws screen with a square at x position reading a var from logic */
renderer->isBusy = false; /* same issue applies if done inside renderer->draw */
Sleep(500); // completely arbitrary, but shows the lack of sync
}
}
void tLogic(Logic* logic, Renderer* renderer) {
unsigned long loops = 0;
while (true) {
++loops;
if (true) {
++(logic->positions[0][0]);
printf("abc"); /* whether or not this printf exists
determines if it's synced */
}
while (renderer->isBusy) {} /* have attempted both to make it a volatile bool and
a std::atomic<bool>, same results either way. */
}
}
int main() {
Window* win = new Window();
Logic* logic = new Logic();
Renderer* renderer = new Renderer(logic); /* i didn't want to use globals, so... */
bool running = true;
while (running) {
std::thread threadLogic(tLogic, logic, renderer);
tRender(renderer, win); /* GLFW rendering did not work in a thread */
threadLogic.join();
}
return 0;
}
实际问题是,如果没有printf
或没有std::cout
(任何一个),屏幕上的可以快速移动,并且绝对不会同步实际渲染。然而,使用printf,它在很大程度上完全同步。除了printf本身之外,每个版本之间的拆卸(根据Visual Studio)都没有改变。我已经检查过,所有编译器优化选项都完全关闭了。问题可能是什么?