首页 > 解决方案 > 如何摆脱 C++ 中的这些线程错误?

问题描述

我用 C++ 制作的这个程序有一些问题。对于我在这里做的事情:

{
    GridWorld world;
    string input = "";

    while (input != "q")
    {
        thread render(&GridWorld::Render, GridWorld());
        render.join();

        input = world.GetInput();

        thread thread_update(&GridWorld::Update, GridWorld());

        thread_update.join();

    }

    cout << "Thank you for playing\n";
}

我收到这些错误

它以前可以工作,但现在已经不行了。

有人可以帮忙吗

标签: c++

解决方案


您是否尝试过使用 lambda 表达式?

...
thread render([]() { GridWorld().Render(); });
...
thread thread_update([]() { GridWorld().Update(); });
...

或者可能

...
thread render([&world]() { world.Render(); });
...
thread thread_update([&world]() { world.Update(); });
...

推荐阅读