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

问题描述 投票:2回答:2

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

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

我决定使用线程和分离,以便可以调用AssetManager的LoadSpriteSheets方法。 (基本上加载XML表并将它们初始化为sprite)

随着游戏状态声明一个结构,它将通过states类传递给任何状态访问结构。返回的内容允许访问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上的线程?或者是需要重写,因为struct是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();
}
c++ multithreading sfml
2个回答
1
投票

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

查看对this postthis 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对象存在。


0
投票

我个人试图尽可能避免多线程,特别是当它与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;
© www.soinside.com 2019 - 2024. All rights reserved.