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

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

本文关键字:线程 创建 一个 存在 调用      更新时间:2023-10-16

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

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

我决定使用该线程并分离,以便可以调用AssetManager的LoadSpritesheets方法。(本质上是加载XML表并将其初始化为Sprites(

在游戏状态声明结构的情况下,它将通过状态类传递,以使任何状态访问结构。作为回报,可以访问AssetManager。

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

在飞溅阶段的interise部分中,线程称为

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

您可以看到,我的想法是将AssetManager(this-> _ data-> Assets(传递到线程,并调用其loadspritesheets方法。它确实这样做了,但它创建了一个新的实例,这是我以后对GetStatus方法进行调查的新实例,从而导致0。(GetStatus本质上获得了LoadSpritesheets方法的进度(,我将使用它来显示一个信息性的加载栏等待应用程序加载的同时。

有没有办法在不初始化新对象的情况下调用AssetManager上的线程?或者是构造是共享_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 <SFMLGraphics.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尚未提供,我不完全确定,但看来您正在调用AssetManager :: loadspritesheets成员在 copy copy AssetManager实例的>,而您要调用AssetManager :: LoadSpritesheets成员在this->_data->assets实例上。

请参阅此帖子的评论和此评论。在构造应该在特定对象上运行成员函数的线程时,您需要将指针,参考包装器或共享_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(线程(:资产= 0x7FB942402808线程完成。data-> Assets = 0x7FB942402808

没有std :: ref((,输出就像:

data-> Assets = 0x7FB5BCC02808(线程(:资产= 0x7FB5BCC02868线程完成。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;