SFML community forums
Help => System => Topic started by: Chunker120 on May 27, 2013, 02:05:33 pm
-
Let's say I have a class called Game, and a public method that looks like this:
void Game::initialize(sf::RenderWindow &winMain);
How would I go about creating a thread that uses this function? I tried these but it didn't work:
sf::Thread loadingThread(&Game::loadWorld, &myGame, winMain);
sf::Thread loadingThread(std::bind(&Game::loadWorld, winMain), &myGame);
-
You have to use std::bind() in combination with std::ref(). They are described on www.cppreference.com.
By the way, you should prefer constructor over public initialize() methods. And of course, write the correct function name ;)
-
If you use C++11 you should prefer std::thread.
-
If you use C++11 you should prefer std::thread.
Yet you did mention that using sfml's thread is a better choice if you want to distribute your code and/or run it on more platforms. My game does indeed use C++11, but I don't know what to pick. Is it fully safe to use std::thread platform-wise?
-
If you want maximum compatibility (i.e. you want your code to compile also on C++03 platforms), then don't use C++11 at all. std::bind is a C++11 function.
Note that this is just about compiling your app on other platforms, not running it. Once it's compiled it can run everywhere.
-
If you want maximum compatibility (i.e. you want your code to compile also on C++03 platforms), then don't use C++11 at all. std::bind is a C++11 function.
Note that this is just about compiling your app on other platforms, not running it. Once it's compiled it can run everywhere.
Well let's say I'll ditch the whole C++11 aspect of it, how would I use sf::Thread in this context, regarding my initial question?
-
You would write a functor.
Or if the compiler supports the TR1 (which all notable ones do since half a decade), you can use std::tr1::bind().