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

Author Topic: Passing variables to ThreadFunction()  (Read 2914 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 17
    • View Profile
Passing variables to ThreadFunction()
« on: December 17, 2011, 05:21:48 pm »
What if I need to pass a variable into the thread function? How can I do that? It's not explained in the tutorial on the SFML website. Two Sprite variables need to be passed into my thread function, so how do I declare that function?
WIll it be void ThreadFunction(sf::Sprite Sprite, sf::Sprite Sprite2);
I keep getting an error, and what is the void *UserData for?
And how do I later create the thread?
In the tutorial it just says:
sf::Thread Thread(&ThreadFunction)


  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Passing variables to ThreadFunction()
« Reply #1 on: December 17, 2011, 05:33:18 pm »
UserData is for passing extra data to the thread. It's a void*, so you can pass a pointer to anything -- but only one thing.

So basically you can do this for example:
Code: [Select]
void ThreadFunction(void* userData)
    sf::Sprite* sprites = static_cast<sf::Sprite*>(userData);

int main()
    sf::Sprites[2] sprites = {sprites1, sprites2};
    sf::Thread thread(&ThreadFunction, sprites);
    return 0;
Laurent Gomila - SFML developer