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

Author Topic: Why does multi-threaded drawing work?  (Read 2211 times)

0 Members and 1 Guest are viewing this topic.

sw

  • Newbie
  • *
  • Posts: 8
    • View Profile
Why does multi-threaded drawing work?
« on: August 12, 2016, 05:50:27 pm »
Hello,

in the SFML tutorial "Drawing 2D stuff" is at the end a section "Drawing from threads", which explains that you can handle all drawing in a seperate thread.
I don't understand why this is working:
I draw for example a sf::Sprite in the rendering thread
Code: [Select]
window.draw(sprite);
which calls sf::RenderTarget::draw and then sf::Sprite::draw. In this draw function I'm accessing the sprites data members m_origin, m_position, m_rotation and so on through the call to sf::Transformable::getTransform().
At the same time i can access this data members from the "main game loop" in another thread through calls like sf::Transformable::setPosition(), sf::Transformable::setRotation() and so on.
Aren't I'm accessing the data members then from different threads at the same time? I have nowhere seen a mutex or so, why does this work?
« Last Edit: August 12, 2016, 05:53:19 pm by sw »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Why does multi-threaded drawing work?
« Reply #1 on: August 12, 2016, 05:59:24 pm »
There's a difference between "work" and "seems to work on this occasion".

The problem with accessing the same variables from different threads is not that they can't access them but they can't access them reliably. For (one simple) example, if one thread is reading a string and half-way through that read, another thread changed that string, what would happen?

"Thread Safety"
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

sw

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Why does multi-threaded drawing work?
« Reply #2 on: August 13, 2016, 02:01:16 pm »
Thank you for your answer.
I already know this about thread safety, i think maybe my first post was misleading. Maybe this question is better:

In the SFML tutorial it looks like it is ok to have no mutex or so. Therefore we would risking that two threads access the same data at the same time. Is this correct? If so, why should we do this, wouldn't it be possible that we will see not "the right things" on the screen the most time when this program is running?
Or is the programmer supposed to add mutexes / locks to prevent this?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Why does multi-threaded drawing work?
« Reply #3 on: August 13, 2016, 04:07:10 pm »
If you are accessing the same data in the main thread and the rendering thread, you will need to secure that access using a lock/mutex system.

The only thing in the example that is accessed by both threads at the same time is the window. I can't say for sure if the window is automatically thread-safe. I guess we should assume so with it being used that way in this example; if it isn't, this example should be changed.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

sw

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Why does multi-threaded drawing work?
« Reply #4 on: August 22, 2016, 05:08:49 pm »
Ok, thanks  :) .