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

标签 c++ multithreading sfml

我有一个使用 SFML 构建的游戏引擎,我试图创建一个在加载 Assets 时更新的加载屏幕。在本例中,它是一个大的 XML 文件。

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

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

随着游戏状态声明一个结构,它通过状态类传递给任何状态访问该结构。这反过来会提供对 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);*/
}

游戏.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,我不能完全确定,但看来您正在对 AssetManager拷贝 调用 AssetManager::LoadSpriteSheets 成员函数。实例,而您想在 this->_data->assets 上调用 AssetManager::LoadSpriteSheets 成员函数实例。

查看对 this post 的评论和 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对象在线程的整个调用过程中都存在。

关于c++ - 在已经存在的类上调用一个线程而不是创建一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49038441/

相关文章:

c++ - QueryPerformanceCounter 和奇怪的结果

c++ - Sprite 表动画期间闪烁

c++ - SFML 在鼠标碰撞测试中崩溃进程终止 255

c++ - 在没有任何纯虚方法的情况下使类抽象

c++ - 如何打印带参数的变量名?

c++ - 如何从具有 userID 和 pageID 的大型日志文件中查找最常访问的 3 个网页序列

c++ - 为什么add函数在c++ 11线程中没有效果?

java - 奇怪的并发代码行为

java - java线程的isAlive()方法不能正常工作?

c++ - SFML 绘图居中文本