c++ - 在 sfml 中不工作线程

标签 c++ multithreading visual-studio sfml

大家好,刚开始我想说我是这方面的新手,很抱歉回答这个基本问题。

#include <SFML/System.hpp>
#include <iostream>

void func()
{
    // this function is started when thread.launch() is called

    for (int i = 0; i < 10; ++i)
        std::cout << "I'm thread number one" << std::endl;
}

int main()
{
    // create a thread with func() as entry point
    sf::Thread thread(&func);

    // run it
    thread.launch();

    // the main thread continues to run...

    for (int i = 0; i < 10; ++i)
        std::cout << "I'm the main thread" << std::endl;

    return 0;
}

这是来自 sfml 官方网站的代码,应该可以打印:

I'm thread number one

I'm the main thread

I'm the main thread

I'm thread number one

I'm thread number one

等等,但控制台首先打印 10 次“我是第一个线程”,然后打印第二个,我不知道为什么。我使用本教程安装 sfml https://youtu.be/axIgxBQVBg0 .

最佳答案

SFML 网站上的示例是潜在 输出的示例。如果您注意到该示例中给出的输出似乎有点不可预测,那是因为它确实如此。你极不可能重现它。作为noted in the comments如果没有进一步的同步,您将无法显式控制线程。

关于c++ - 在 sfml 中不工作线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51507890/

相关文章:

multithreading - Spring Batch多线程抛出java.lang.Thread.State

c++我可以声明类成员并在构造函数中初始化它吗

c++ - MPI中每个进程的随机数

.net - parameterizedThreadstart、Threadstart和Thread的区别

android - 在 MPAndroidChart 中绘制图形完成时如何接收回调?

asp.net - 我无法运行该项目,在 Visual Studio 中出现错误 STG_E_FILENOTFOUND

visual-studio - 如何在具有默认参数的插槽上使用 Qt5 connect()

c# - 将自定义控件 dll 添加到 Visual Studio 工具箱

c++ - 玩具语言的自定义分配器

java - 将cpp函数指针转换为java接口(interface)方法