c++ - 如何在类里面使用线程?

标签 c++ multithreading opengl

我正在尝试为我的 OpenGL 项目创建一个加载屏幕,并已阅读该内容以使其正常工作,最好使用线程。我试图用我的线程调用我的函数,但我不断收到这些错误:

error C2064: term does not evaluate to a function taking 3 arguments

IntelliSense: no instance of constructor "std::thread::thread" matches the argument list argument types are: (void (Screen* newScreen, bool activeVisuals, bool activeControls), PlayScreen *, bool, bool)

这是我的代码:

//LoadingScreen
class LoadingScreen
{
    LoadingScreen();
    void LoadNewScreen(Screen* newScreen, bool activeVisuals, bool activeControls);
    void Setup();
};

void LoadingScreen::LoadNewScreen(Screen* newScreen, bool activeVisuals, bool activeControls)
{

}

void LoadingScreen::Setup()
{
    PlayScreen *playScreen = new PlayScreen();
    std::thread first(LoadingScreen::LoadNewScreen,playScreen, true, true);// , playScreen, true, true);

    first.join();
}

//source.cpp
LoadingScreen loadingScreen;
int main()
{
    LoadingScreen loadingScreen = LoadingScreen();
    loadingScreen.Setup();

    return 0;
}

最佳答案

您需要传递一个附加参数,该参数是其成员函数作为第一个参数传递的类的实例。

std::thread first(&LoadingScreen::LoadNewScreen, this, playScreen, true, true);
                                             //  ^^^^ <= instance of LoadingScreen

需要额外的参数,因为这是实际调用 LoadNewScreen 的参数。

this->LoadNewScreen(playScreen, true, true);

关于c++ - 如何在类里面使用线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28574004/

相关文章:

java - spring集成中的几个任务执行器

c++ - 如何更改 OpenGL 中的视角?

c++ - 使用 libdc1394 在 Pointgrey Firefly MV 上输出频闪信号

Python 池 生成池

c++ - 预期时间复杂度为 O(n^2),但结果为 O(n)。有人可以解释一下为什么吗?

java - TimerTask 中的 NetworkOnMainThreadException

qt - OpenGL 或 OpenGL ES

c++ - OpenGL glLinkProgram 返回 false 但信息日志为空;检查了一切

c++ - SSL 与加密套接字

c++ - 黑莓 10 : GNU STL