嗨,首先,我想说我是新来的,对于这个基本的问题,我很抱歉。
#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外部站点的代码,它应该是打印的:
我是第一线 我是主线 我是主线 我是第一线 我是第一线
等等,但控制台打印的前10倍“我是线程第一”和第二个,我不知道为什么。我使用本教程安装sfml https://youtu.be/axIgxBQVBg0。
发布于 2018-10-15 21:11:50
SFML网站的例子是潜在输出的一个例子。如果您注意到在这个例子中给出的输出看起来有点不可预测,那是因为它是不可预测的。你不太可能复制它。作为noted in the comments,如果没有进一步的同步,您将无法显式地控制线程。
https://stackoverflow.com/questions/51507890
复制相似问题