在已经存在的 class 上调用一个线程而不是创建一个

Call a thread on an already existing class rather than creating one

我有一个使用 SFML 构建的游戏引擎,我试图创建一个在加载资产时更新的加载屏幕。在这种情况下,它是一个大 XML 文件。

我想使用一个线程,因为这个过程大约需要 10 秒才能完成,因此应用程序会卡住直到完成。

我决定使用线程并分离,以便可以调用 AssetManager 的 LoadSpriteSheets 方法。 (本质上加载一个 XML sheet 并将它们初始化为精灵)

随着游戏状态声明一个结构,它通过状态 class 传递给任何状态访问该结构。 returns 中的哪一个可以访问 AssetManager。

struct GameData
{
    StateMachine machine;
    sf::RenderWindow window;
    AssetManager assets;
    InputManager input;
};

在 SplashStage 的初始化部分调用线程

std::thread t1(&AssetManager::LoadSpriteSheets, this->_data->assets);
t1.detach();

如您所见,我的想法是将AssetManager (this->_data->assets) 传递给线程并调用其LoadSpriteSheets 方法。它确实这样做了,但它创建了一个新的 AssetManager 实例,导致我后来对 GetStatus 方法的轮询结果为 0。(GetStatus 本质上获取 LoadSpriteSheets 方法的进度)我打算用它来显示一个信息加载栏在等待应用程序加载时。

有没有一种方法可以在不初始化新对象的情况下调用 AssetManager 上的线程?或者是否需要重写,因为结构是 shared_ptr。

如果您看到我正在努力实现的更好的解决方案,也请随时纠正我。

相关类:

#include "SplashState.h"
#include <iostream>
#include <sstream>
#include <thread>

#include "AssetManager.h"

#include "Definitions.h"
SplashState::SplashState(GameDataRef data) : _data(data)
{

}

void SplashState::Init()
{
    this->_data->assets.LoadTexture("Splash State Background", SPLASH_STATE_BACKGROUND_FILEPATH);
    _background.setTexture(this->_data->assets.GetTexture("Splash State Background"));



    std::thread t1(&AssetManager::LoadSpriteSheets, this->_data->assets);
    t1.detach();


    std::cout << "Completed In Splash" << std::endl;
    /*_test.setTexture(this->_data->assets.GetTexture("spaceShips_001.png"));
    _test.setPosition((SCREEN_WIDTH / 2) - (_test.getGlobalBounds().width / 2), _test.getGlobalBounds().height / 2);*/

    //std::thread t2(&SplashState::LoadXML, this);

    /*_test.setTexture(this->_data->assets.GetTexture("spaceAstronauts_001.png"));
    _test.setPosition((SCREEN_WIDTH / 2) - (_test.getGlobalBounds().width / 2), _test.getGlobalBounds().height / 2);*/
}

Game.cpp

#pragma once
#include <memory>
#include <string>
#include <SFML\Graphics.hpp>
#include "StateMachine.h"
#include "AssetManager.h"
#include "InputManager.h"

struct GameData
{
    StateMachine machine;
    sf::RenderWindow window;
    AssetManager assets;
    InputManager input;
};

typedef std::shared_ptr<GameData> GameDataRef;

class Game
{
public:
    Game(int width, int height, std::string title);

private: 
    const float dt = 1.0f / 60.0f;
    sf::Clock _clock;

    GameDataRef _data = std::make_shared<GameData>();

    void Run();
};

#include "Game.h"
#include "SplashState.h"

Game::Game(int width, int height, std::string title)
{
    _data->window.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);
    _data->machine.AddState(StateRef(new SplashState(this->_data)));
    this->Run();
}

由于未提供 AssetManager::LoadSpriteSheets,我不能完全确定,但您似乎正在对 copy 调用 AssetManager::LoadSpriteSheets 成员函数AssetManager 实例的,而您想在 this->_data->assets 实例上调用 AssetManager::LoadSpriteSheets 成员函数。

查看 this post and this comment 的评论。当构造一个线程应该 运行 特定对象的成员函数时,您需要将指针、引用包装器或 shared_ptr 传递给该对象。实现此目的的一种方法是使用 std::ref():

#include <functional>
#include <iostream>
#include <thread>
#include <utility>

struct AssetManager {
    AssetManager() { }
    virtual ~AssetManager() { }

    void LoadSpriteSheets() {
        std::cout << "(in thread): assets = " << static_cast<const void*>(this) << "\n";
    }
};

struct GameData
{
    AssetManager assets;
};

typedef std::shared_ptr<GameData> GameDataRef;

int main() {
    GameDataRef data = std::make_shared<GameData>();

    std::cout << "data->assets = " << static_cast<const void*>(&data->assets) << "\n";

    std::thread t1(&AssetManager::LoadSpriteSheets, std::ref(data->assets));
    t1.join();

    std::cout << "thread finished.\n";

    std::cout << "data->assets = " << static_cast<const void*>(&data->assets) << "\n";

    return 0;
}

这将输出如下内容:

data->assets = 0x7fb942402808
(in thread): assets = 0x7fb942402808
thread finished.
data->assets = 0x7fb942402808

没有std::ref(),输出是这样的:

data->assets = 0x7fb5bcc02808
(in thread): assets = 0x7fb5bcc02868
thread finished.
data->assets = 0x7fb5bcc02808

(注意AssetManager实例在线程内是不同的)

注意确保 AssetManager 对象在整个线程调用过程中都存在。

就个人而言,我会尽可能避免多线程,尤其是当它与 OpenGL 相关时,因为最终大多数事情都必须以某种方式再次同步,这会导致您(以及您的)无法直接看到的开销无论如何,典型的加载屏幕不应该太复杂)。

我通常使用的方法是特定的加载状态,它将继续在屏幕上绘制并尝试在屏幕更新之间加载(或下载!)尽可能多的资产。

这是记忆中的,基本上只是为了展示概念:

sf::Clock timer;
std::queue<std::string> assets;
std::size_t numAssets = 0;

// to queue some file for loading/processing
assets.push("/path/to/my/file.xml");
++numAssets;

// inside the main loop/state update
timer.restart();
do {
    loadMyAsset(assets.front());
    assets.pop();
} while (!assets.empty() && mTimer.getEllapsedTime() < sf::milliseconds(10))

// drawing
const std::size_t progress = 100 - 100 * assets.size() / numAssets;