SFML community forums

Help => System => Topic started by: vechestva on March 27, 2014, 12:42:29 am

Title: How to create a thread(with args) from the method?
Post by: vechestva on March 27, 2014, 12:42:29 am
Hi!

How to create a thread(with args) from the method?

class A
{
public:
    A()
    {
         // understood that does not work
         pThread = new sf::Thread(std::bind(&Method, 15, 25), this));
    }
    ~A()
    {
         delete pThread;
    }
    void Method(int a, int b);

    private:
        sf::Thread* pThread;
};
 
Title: Re: How to create a thread(with args) from the method?
Post by: Laurent on March 27, 2014, 07:51:47 am
Try this:

pThread = new sf::Thread(std::bind(&A::Method, this, 15, 25)));

And since you have std::bind, I guess you also have std::thread; why don't you use it?
Title: Re: How to create a thread(with args) from the method?
Post by: Nexus on March 27, 2014, 09:40:58 am
I don't think you need pointers here. Use RAII (http://www.bromeon.ch/articles/raii.html), it makes your code simpler and correct (because currently it's not, see Rule Of Three):
class A
{
public:
    A() : Thread(std::bind(&A::Method, this, 15, 25)) {}
    // no need for destructor

    void Method(int a, int b);

    private:
        sf::Thread pThread;
};
Title: Re: How to create a thread(with args) from the method?
Post by: vechestva on March 27, 2014, 05:57:57 pm
Thanks to all! ;D