0

我正在开发一个游戏。假设有一个带有这些方法(以及其他一些方法)的对象LoadingState :

  • 创造
  • 更新
  • 加载

每次 CPU 时钟滴答时都会调用更新,而创建只会调用一次,它应该调用加载函数(异步),以加载一些游戏资产。在 create 函数中异步调用 load 允许(理论上)在 create/load 执行时从调用的 beling 更新。然而,这并没有发生。

在 Visual Studio 2015 之前,我像这样使用 std::async :

std::async(&LoadingState::load, this, THE_ASSETS_PATHS_STRING_VECTOR);

在迁移到 Visual Studio 2015 (C++17) 并阅读必须指定 std::launch 之后,否则可能会发生意外行为,现在异步调用如下:

std::async(std::launch::async, &LoadingState::load, this, THE_ASSETS_PATHS_STRING_VECTOR);

换句话说,在我看来,'this' 被 std::async 锁定,阻塞了整个对象,阻止了主线程调用更新。

更多相关代码:

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:在迁移之前,一切都可以顺利运行。

4

1 回答 1

2
std::async(std::launch::async, &LoadingState::load, this, assets);

future返回的 by将async 在其析构函数中阻塞,直到async函数完成(与其他所有 future对象不同)。因此,您必须捕获它future并使其保持活力(在必要时移动它),直到您准备好回答。

或者您可以停止使用async.

读到必须指定 std::launch

不,不是的。

于 2016-07-14T18:49:09.807 回答