16
General / Re: Can't figure out how to work with std::thread and SFML
« on: May 31, 2013, 11:35:03 am »This code is not complete nor minimal. Please read this: http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368
A typical minimal example for this kind of error would be the following:#include <thread>
void f(int) {}
int main()
{
std::thread t(f, 5);
return 0;
}
Well it's not complete because the rest doesn't matter, it's just a bunch of functions for handling input and game logic, and since it's a large game it would take up a lot of useless space, it's the only code that can be troubled, the rest has absolutely no issues which is why I didn't want to bother you with it. Here's another attempt, though the code above basically contains the same thing. Also I can't post the whole source code to make the code runable, I have 14 files, and using all of them in the same file where this problem occurs. This is basically the only line of code that can possibly cause this issue.
#include <functional>
#include <thread>
void loadWorld(sf::RenderWindow &winMain) {};
int main()
{
sf::RenderWindow winMain;
std::thread loadingThread(loadWorld, winMain);
winMain.setSize(sf::Vector2u(SCREEN_W, SCREEN_H));
winMain.setTitle("Council of Torment");
icon.loadFromFile("resources\\icon.png");
winMain.setIcon(50, 50, icon.getPixelsPtr());
winMain.setFramerateLimit(60);
loadingThread.join();
}
#include <thread>
void loadWorld(sf::RenderWindow &winMain) {};
int main()
{
sf::RenderWindow winMain;
std::thread loadingThread(loadWorld, winMain);
winMain.setSize(sf::Vector2u(SCREEN_W, SCREEN_H));
winMain.setTitle("Council of Torment");
icon.loadFromFile("resources\\icon.png");
winMain.setIcon(50, 50, icon.getPixelsPtr());
winMain.setFramerateLimit(60);
loadingThread.join();
}
I'm sorry if this wasn't good either, but it's the only piece of code that can actually have an error, there's no point in showing anything more or less, because it's just two lines of code, which cause the error. Replacing std::Thread with sf::Thread would probably fix it, since that's what I've been using up to the point when I heard std::thread is better.
But supposing the code is not good, how would you make a thread that runs my kind of function?