Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to set a class method with paramaters as a thread function?  (Read 4705 times)

0 Members and 1 Guest are viewing this topic.

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
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);

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to set a class method with paramaters as a thread function?
« Reply #1 on: May 27, 2013, 02:14:20 pm »
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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to set a class method with paramaters as a thread function?
« Reply #2 on: May 27, 2013, 02:19:05 pm »
If you use C++11 you should prefer std::thread.
Laurent Gomila - SFML developer

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: How to set a class method with paramaters as a thread function?
« Reply #3 on: May 27, 2013, 03:36:19 pm »
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to set a class method with paramaters as a thread function?
« Reply #4 on: May 27, 2013, 03:52:03 pm »
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.
Laurent Gomila - SFML developer

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: How to set a class method with paramaters as a thread function?
« Reply #5 on: May 27, 2013, 03:58:11 pm »
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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to set a class method with paramaters as a thread function?
« Reply #6 on: May 27, 2013, 04:06:21 pm »
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().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything