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

Author Topic: [SOLVED]Running class functions with an object with a Thread  (Read 2878 times)

0 Members and 1 Guest are viewing this topic.

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED]Running class functions with an object with a Thread
« on: October 15, 2011, 09:58:17 pm »
What syntax would I use to run a class function as a Thread with an object and a parameter? For example:

Code: [Select]

Thread thread(foo.bar(param));

thread.Launch();


Thanks in advance.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED]Running class functions with an object with a Thread
« Reply #1 on: October 15, 2011, 10:02:55 pm »
You can't do that directly, but you can use std::bind() or boost::bind():
Code: [Select]
class Foo { void bar(int x); };
Foo foo;

Thread thread( std::bind(&Foo::bar, &foo, 7) );

See the Boost.Bind documentation. The alternative is a self-written functor with operator() or a lambda expression in C++11.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED]Running class functions with an object with a Thread
« Reply #2 on: October 15, 2011, 10:17:34 pm »
What do I #include to use std::bind?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED]Running class functions with an object with a Thread
« Reply #3 on: October 15, 2011, 10:28:10 pm »
The <functional> header. Your standard library must support C++11, though (VS 2010 or a newer g++ with the flag -std=c++0x). Otherwise, use the Boost or TR1 version.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: