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

Author Topic: [SOLVED] Is sf::Drawable::draw thread safe?  (Read 2582 times)

0 Members and 2 Guests are viewing this topic.

samsonch

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] Is sf::Drawable::draw thread safe?
« on: March 20, 2013, 06:57:12 pm »
Hi,  :D
As mentionned in the title, I wonder if sf::Drawable::draw thread safe.

I created a class derived from sf::Drawable for rendering. It is designed to be executed in parallel with computation. The reason is that computation take to mush time to be executed and I don't want that rendering be frozen during this time. The class has a method called "SetFrame(...)" to pass data from the computation thread to the rendering thread, including textures for some sprites.

To be thread safe, I used some mutex. But, for the sprites, I can't use mutex because the draw method doesn't permit changes (const keyword). :-\

So, is sf::Drawable::draw already thread safe, or I must find an other way?

Thanks  :D
« Last Edit: March 21, 2013, 04:39:51 pm by samsonch »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is sf::Drawable::draw thread safe?
« Reply #1 on: March 20, 2013, 08:42:40 pm »
If I understand correctly, your question is not "is draw thread-safe", but rather "can draw be called in parallel with xxx" (your initial question means that you're calling draw in parallel from several threads on the same object).

So my question is: what do you do in parallel to draw? Which functions do you call in the computation thread?
Laurent Gomila - SFML developer

samsonch

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is sf::Drawable::draw thread safe?
« Reply #2 on: March 20, 2013, 09:15:47 pm »
Sorry for the confusion. I'll try to be more specific.

The computation thread call an user defined method to change the texture of a sprite. (It's not my real code but it's in the spirit)
void MyClass::SetFrame(sf::Texture& texture)
{
        m_Sprite.setTexture(texture);
}

The rendering thread call the declaration of the draw method.
void MyClass::draw (sf::RenderTarget &target, sf::RenderStates states) const
{
        target.draw(m_Sprite, states);
}

So, maybe it may occur a collision when the first thread update the texture and the other display the sprite at the same time. This collision can it occur? And if so, how can I avoid it?

Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is sf::Drawable::draw thread safe?
« Reply #3 on: March 20, 2013, 09:57:40 pm »
Nothing is thread-safe in SFML, so problems might indeed occur when doing this. The solution is to use a mutex. Declare it with the mutable keyword if you want to be able to use it in a const member function.

By the way, is the texture always the same instance, or do you pass a new one every time you call SetFrame? If it's always the same, don't forget to protect it too (and if it's the same, you don't need to call SetTexture every time).
« Last Edit: March 20, 2013, 09:59:34 pm by Laurent »
Laurent Gomila - SFML developer

samsonch

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] Is sf::Drawable::draw thread safe?
« Reply #4 on: March 21, 2013, 04:39:16 pm »
 I had not thought of using the mutable keyword...
Thanks, this answers my question perfectly!  :D

 

anything