# Does updating a renderable's position while rendering it cause race condition? (Chapter 9 Scene Graph)

**URL:** <https://forums.kodeco.com/t/does-updating-a-renderables-position-while-rendering-it-cause-race-condition-chapter-9-scene-graph/135592>\
**Category:** Metal by Tutorials\
**Created:** [March 19, 2021, 3:49pm UTC](https://forums.kodeco.com/t/does-updating-a-renderables-position-while-rendering-it-cause-race-condition-chapter-9-scene-graph/135592 "2021-03-19T15:49:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miraco](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/miraco/32/102075_2.png) [@miraco](https://forums.kodeco.com/u/miraco)\
**Post date:** [March 19, 2021, 3:49pm UTC](https://forums.kodeco.com/t/does-updating-a-renderables-position-while-rendering-it-cause-race-condition-chapter-9-scene-graph/135592/1 "2021-03-19T15:49:10Z")

</div>

In the final project of Scene Graph, we have a function called `func keyPressed(key: KeyboardControl, state: InputState) -> Bool` where we updates the car’s position:

`car.position = [0.35, -1, 0.1]`  
`car.rotation = [0, 0, 0]`

But we also constantly read the car’s position while we are rendering it.

Will this cause race condition? Do we need to avoid it?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [March 20, 2021, 12:40am UTC](https://forums.kodeco.com/t/does-updating-a-renderables-position-while-rendering-it-cause-race-condition-chapter-9-scene-graph/135592/2 "2021-03-20T00:40:36Z")

</div>

This isn’t a race condition.

`Scene`’s `update(deltaTime:)` is called once per frame. It calls `InputController`’s `updatePlayer(deltaTime:)` which is where the position gets updated.

In a different thread, `processEvent(key:state:)` processes the keyboard input. `directionKeysDown` is a `Set`, so if you repeatedly press the `w` key, only one `w` value is retained.

`InputController`’s `updatePlayer(deltaTime:)` (being performed once per frame) takes that one `w` value and updates `position`.

Also, pressing a key is an I/O input signal sent to the CPU and has the lowest priority so it will be queued to be executed when all writes have finished. Also, CPU and GPU are so fast, that by the time you pressed `w` twice, multiple frames have already happened.

---

<div class="post-metadata">

**Author:** ![miraco](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/miraco/32/102075_2.png) [@miraco](https://forums.kodeco.com/u/miraco)\
**Post date:** [March 20, 2021, 1:09am UTC](https://forums.kodeco.com/t/does-updating-a-renderables-position-while-rendering-it-cause-race-condition-chapter-9-scene-graph/135592/3 "2021-03-20T01:09:30Z")

</div>

I see. What if the car’s position is updated in `func updateScene(deltaTime: Float)` for every frame, without pressing any key or triggering pan gestures?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [March 20, 2021, 2:00am UTC](https://forums.kodeco.com/t/does-updating-a-renderables-position-while-rendering-it-cause-race-condition-chapter-9-scene-graph/135592/4 "2021-03-20T02:00:35Z")

</div>

These updates are triggered by `Scene`’s `update(deltaTime:)`, which happens once per frame before the draw. Whether you set the position in `updatePlayer(deltaTime:)` or `updateScene(deltaTime)`, it doesn’t make any difference.

Also, `position` itself is not passed to the GPU. It is part of the model matrix that is passed.

Also, `commandBuffer.waitUntilCompleted()` at the end of `draw(in:)`, means that everything stops until the GPU finishes its render. This isn’t the best way to synchronize between CPU and GPU.

Triple buffering (introduced later in the book) will solve this.

[https://developer.apple.com/documentation/metal/synchronization/synchronizing\_cpu\_and\_gpu\_work](https://developer.apple.com/documentation/metal/synchronization/synchronizing_cpu_and_gpu_work)
