SFML community forums

General => General discussions => Topic started by: Laurent on November 20, 2010, 02:26:31 pm

Title: Extended API for sf::Thread in SFML 2
Post by: Laurent on November 20, 2010, 02:26:31 pm
Hi

I changed the sf::Thread class in SFML 2 to make it more flexible. I removed the Run() virtual function (so, no more inheritance from sf::Thread) and added a bunch of template constructors instead.

So now you can do this:
Code: [Select]
void f();
sf::Thread t(&f);

Code: [Select]
void f(int arg); // arg type can be anything
sf::Thread t(&f, 5);

Code: [Select]
class Task
{
public:

    void run();
};
Task task;
sf::Thread t(&Task::run, &task);

Code: [Select]
struct Functor
{
    void operator()();
};
sf::Thread t(Functor());

Code: [Select]
struct Functor
{
    void operator()(std::string arg);
};
sf::Thread t(Functor(), "toto");

Code: [Select]
sf::Thread t(boost::bind(...));

So the previous scenarios are still possible, plus a lot of new ones.
Title: Extended API for sf::Thread in SFML 2
Post by: Groogy on November 20, 2010, 02:42:20 pm
That looks really good making it also more generic, object-oriented and easier to create synchronisation nodes.
Title: Extended API for sf::Thread in SFML 2
Post by: nfries88 on November 20, 2010, 05:19:52 pm
This will be a great convenience. Thanks.
Title: Extended API for sf::Thread in SFML 2
Post by: Nexus on November 21, 2010, 03:53:02 pm
Nice to see sf::Thread go from the Java inheritance approach towards function objects which are much more flexible and require less boilerplate-code! :)