我正在开发一款游戏。可以说,那里有一个对象LoadingState进行这些方法(和一些其他):C++ 17异步:运行这个'方法阻塞整个对象
- 创建
- 更新
- 负载
的更新被称为每个CPU的时钟滴答的时间,而创建只会被调用一次,它应该调用加载函数(异步),以加载一些游戏资源。在创建函数内异步调用加载允许(理论上)从创建/加载执行时调用的beling进行更新。但是,这并没有发生。
之前到Visual Studio 2015年,我用的std ::异步那样:
std::async(&LoadingState::load, this, THE_ASSETS_PATHS_STRING_VECTOR);
迁移到Visual Studio 2015年(C++ 17)和阅读的标准::启动必须是经过指定,否则无法预期的行为可能发生,异步现在叫这样的:
std::async(std::launch::async, &LoadingState::load, this, THE_ASSETS_PATHS_STRING_VECTOR);
换句话说,它看起来像对我说,“这”被通过的std ::异步锁定,阻止整个对象,阻止主线程调用更新。
更多相关代码:
void LoadingState::onCreate()
{
std::vector<std::string> assets;
assets.push_back("Images/Scenario/wall.png");
assets.push_back("Images/Scenario/bigdummy.png");
assets.push_back("Images/Scenario/buildings.png");
assets.push_back("Images/Scenario/floor.png");
assets.push_back("Images/Scenario/terrain.png");
assets.push_back("Images/Scenario/trees.png");
// Load assets asynchronously
std::async(std::launch::async, &LoadingState::load, this, assets);
}
void LoadingState::load(std::vector<std::string> assets)
{
unsigned int count = 0;
unsigned int ratio = 100U/assets.size();
// Cache the asset accordingly
for (auto& asset : assets)
{
// Load and store the asset
// ...
// Asset loaded
count++;
// Calculate the progress by count
m_progress = count * ratio;
}
// If assets were skipped or progress would be like 98% due to truncation, normalize it
m_progress = 100U;
}
void LoadingState::update(float delta)
{
// ...
// If finished loading the resources, move on to playing state!
if (m_progress >= 100U) {
m_machine->next(new PlayingState(m_machine));
}
}
什么我误解在这里?
PS:在迁移之前用于运行的所有东西。
Priort to C++ 11,我保证你没有使用'std :: async'! –
THE_ASSETS_PATHS_STRING_VECTOR也不存在。你的批评/讽刺并没有帮助。 –
“* Visual Studio 2015(C++ 17)*”VS 2015不包含C++ 17。它至多包含C++ 17 *的位。 –