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

Author Topic: How to create a thread(with args) from the method?  (Read 3791 times)

0 Members and 1 Guest are viewing this topic.

vechestva

  • Newbie
  • *
  • Posts: 25
    • View Profile
How to create a thread(with args) from the method?
« 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;
};
 
I do not know much English.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to create a thread(with args) from the method?
« Reply #1 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?
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to create a thread(with args) from the method?
« Reply #2 on: March 27, 2014, 09:40:58 am »
I don't think you need pointers here. Use RAII, 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;
};
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vechestva

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: How to create a thread(with args) from the method?
« Reply #3 on: March 27, 2014, 05:57:57 pm »
Thanks to all! ;D
I do not know much English.