gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:04 25 4
gpt4 key购买 nike

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

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com