问题描述
我正在尝试将多线程块加载添加到我的体素游戏中。我的目标是创建一个不断循环遍历块的线程,如果块需要更新,它将从线程中更新。现在,我只是在游戏的 init 函数中执行一次更新块的函数。这是应该执行此操作的代码:
void myFunction(World* world,const std::vector<vec3>& colors){
Utils::log("updating the chunks");
world->updateChunks(colors);
std::this_thread::sleep_for(100ms);
}
void Game::init(GUIFont* font,sf::IpAddress ip){
connectToServer(ip);
receiveAndDecompressWorld();
m_cubeMap.init();
m_particleRenderer.init();
m_handler.init(font);
m_camera.init(glm::vec3((Constants::getLocalWorldWidth() * Constants::getChunkWidth()) / 2,Constants::getChunkWidth(),(Constants::getLocalWorldWidth() * Constants::getChunkWidth()) / 2));
initGUI();
generateColorVector(colors);
std::thread mythread(myFunction,&world,colors);
mythread.join();
}
上面的代码编译并运行,事实上我尝试在 updateChunks()
函数中放置一个 cout 语句,它确实打印到屏幕上,但顶点没有上传到 vao 并且没有任何网格完全被吸引。作为参考,此代码按预期工作:
void Game::init(GUIFont* font,(Constants::getLocalWorldWidth() * Constants::getChunkWidth()) / 2));
initGUI();
generateColorVector(colors);
world.updateChunks(colors);
}
我在这里所做的没有“意义”。我只想(现在)能够在与主线程不同的线程上运行 updateChunks()
函数。所以最后一个问题是:为什么 updateChunks()
函数不能在单独的线程上正常运行?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)