SFML community forums

Help => System => Topic started by: Pyrius on October 15, 2011, 09:58:17 pm

Title: [SOLVED]Running class functions with an object with a Thread
Post by: Pyrius 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.
Title: [SOLVED]Running class functions with an object with a Thread
Post by: Nexus 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 (http://www.boost.org/doc/libs/1_47_0/libs/bind/bind.html). The alternative is a self-written functor with operator() or a lambda expression in C++11.
Title: [SOLVED]Running class functions with an object with a Thread
Post by: Pyrius on October 15, 2011, 10:17:34 pm
What do I #include to use std::bind?
Title: [SOLVED]Running class functions with an object with a Thread
Post by: Nexus 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.