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

Author Topic: Providing class member function to thread?  (Read 7908 times)

0 Members and 1 Guest are viewing this topic.

tomas

  • Newbie
  • *
  • Posts: 3
    • View Profile
Providing class member function to thread?
« on: July 08, 2009, 05:29:44 pm »
I want to run a class member function in a separate thread. Is there any straightforward way of doing this? An hour of googling and trial-and-error experimenting hasn't helped me in this case. It is obviously not as easy as

Code: [Select]
sf::Thread Thread(&MyObject.MyFunction);.

I tried things like

Code: [Select]
typedef void (SomeClass::*fptr)();
fptr ptrMyFunction;
ptrMyFunction = &SomeClass::MyFunction;


to get the member function pointer, but it doesn't seem to work.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Providing class member function to thread?
« Reply #1 on: July 08, 2009, 05:38:49 pm »
You can't. sf::Thread only accepts free function pointers.

But that's the C-style way of using it, you'd better make your class inherit from it (see the tutorial for more informations).
Laurent Gomila - SFML developer

tomas

  • Newbie
  • *
  • Posts: 3
    • View Profile
Providing class member function to thread?
« Reply #2 on: July 08, 2009, 05:47:45 pm »
OK, I see. I solved it by having a wrapper function with an infinity loop that calls the class member function instead.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Providing class member function to thread?
« Reply #3 on: July 08, 2009, 05:50:10 pm »
Looks like an ugly solution. Why don't you make your class privately inherit from sf::Thread instead?
Laurent Gomila - SFML developer

tomas

  • Newbie
  • *
  • Posts: 3
    • View Profile
Providing class member function to thread?
« Reply #4 on: July 08, 2009, 05:55:56 pm »
Hmm, I wouldn't know how to do that right away, not that experienced with C++. I will look at the tutorials again and try to figure out a good solution. Thanks for the tip.

EDIT: Implemented it in that way now. Works great and is a pretty solution. Thanks.