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

Author Topic: How to use threads with class member functions.  (Read 4824 times)

0 Members and 1 Guest are viewing this topic.

Howitzer21

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to use threads with class member functions.
« on: February 15, 2012, 02:00:41 am »
How do you use threads with class member functions?  If I pass a function pointer to a global function to a new Thread object, that works fine.  If I try to pass a function pointer to a member function inside of a class, that doesn't work out too well.  What's the syntax for it?  Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to use threads with class member functions.
« Reply #1 on: February 15, 2012, 08:04:27 am »
SFML 1.6 or 2?
Laurent Gomila - SFML developer

Howitzer21

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to use threads with class member functions.
« Reply #2 on: February 15, 2012, 04:52:19 pm »
1.6

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to use threads with class member functions.
« Reply #3 on: February 15, 2012, 05:19:34 pm »
Ok. So just read the tutorial please ;)
Laurent Gomila - SFML developer

Howitzer21

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to use threads with class member functions.
« Reply #4 on: February 15, 2012, 05:29:26 pm »
I DID read the tutorial.  I practiced it and everything.  There was stuff in there for using them with function pointers to globally-defined functions, as well as inheriting a class from them.  But as for not using inheritance and just passing a function pointer to a class function, there's really no reference there.  I've tried it myself and haven't been able to get it to wrok.

Howitzer21

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to use threads with class member functions.
« Reply #5 on: February 15, 2012, 05:34:45 pm »
Well, I did manage to get it working with static functions, but what if I want to use a function specifically associated with that object without having the class inherit from Thread?  Is there a way to do this?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How to use threads with class member functions.
« Reply #6 on: February 15, 2012, 05:37:44 pm »
Quote from: "Howitzer21"
But as for not using inheritance and just passing a function pointer to a class function, there's really no reference there.
Because there is no possibility. Function pointers and member function pointers are not compatible to each other.

If I remember the old API correctly, you can use the void* to call indirectly a class member function. But you'd probably better use SFML 2.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Howitzer21

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to use threads with class member functions.
« Reply #7 on: February 15, 2012, 05:43:31 pm »
Thanks, Nexus!