# Inexplainable race condition in SDL Renderer

**URL:** <https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454>\
**Category:** SDL Development\
**Created:** [October 22, 2022, 11:52am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454 "2022-10-22T11:52:07Z")\
**Posts on this page:** 15\
**Page:** 3

<div class="post-metadata">

**Author:** ![andreas\_g](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@andreas\_g](https://discourse.libsdl.org/u/andreas_g)\
**Post date:** [October 28, 2022, 11:48am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/41 "2022-10-28T11:48:32Z")

</div>

You will need the latest development version of SDL2. Or you can just remove the call to SDL\_GetWindowSizeInPixels(). It is only required for proper window resize and not relevant for the issue.

---

<div class="post-metadata">

**Author:** ![Sanette](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sanette/32/2749_2.png) [@Sanette](https://discourse.libsdl.org/u/Sanette)\
**Post date:** [October 28, 2022, 3:59pm UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/42 "2022-10-28T15:59:51Z")

</div>

ok I was able to compile!

But. I don’t see your bug 😅

---

<div class="post-metadata">

**Author:** ![andreas\_g](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@andreas\_g](https://discourse.libsdl.org/u/andreas_g)\
**Post date:** [October 28, 2022, 4:45pm UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/43 "2022-10-28T16:45:03Z")

</div>

That is what makes this issue so annoying. It is highly dependent on timings. If I run the application in debug mode (slower) I see the problem way more often than when running it in normal mode. I don‘t think the bug is in my code, but maybe someone can see a mistake when reading through it? Maybe some SDL developer can check if there is something in SDL that might cause this?

---

<div class="post-metadata">

**Author:** ![sjr](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sjr/32/5749_2.png) [@sjr](https://discourse.libsdl.org/u/sjr)\
**Post date:** [October 29, 2022, 2:40am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/44 "2022-10-29T02:40:04Z")

</div>

Is it possible you aren’t setting `blitUI` everywhere you need to? Or that something is clearing it?

---

<div class="post-metadata">

**Author:** ![andreas\_g](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@andreas\_g](https://discourse.libsdl.org/u/andreas_g)\
**Post date:** [October 29, 2022, 4:33am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/45 "2022-10-29T04:33:59Z")

</div>

No, all relevant parts of the code are reached.

---

<div class="post-metadata">

**Author:** ![rtrussell](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/rtrussell/32/4768_2.png) [@rtrussell](https://discourse.libsdl.org/u/rtrussell)\
**Post date:** [October 29, 2022, 9:20am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/46 "2022-10-29T09:20:26Z")

</div>

I know this isn’t really helpful, but I’ve not seen any evidence of a similar issue in my app (which is also a kind of emulator) on multiple platforms - Windows, MacOS, Linux, Android, iOS and in-browser (Emscripten) - nor have any of my users reported it.

However I’m using the OpenGL backend everywhere (so if it’s related to Direct3D or Metal, I won’t have encountered it) and I’m not running anything later than SDL 2.0.22 on any of those platforms.

---

<div class="post-metadata">

**Author:** ![ROSY](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@ROSY](https://discourse.libsdl.org/u/ROSY)\
**Post date:** [October 29, 2022, 10:52am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/47 "2022-10-29T10:52:15Z")

</div>

Have you checked whether this game of multithreading gives real acceleration?

---

<div class="post-metadata">

**Author:** ![andreas\_g](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@andreas\_g](https://discourse.libsdl.org/u/andreas_g)\
**Post date:** [October 29, 2022, 3:52pm UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/48 "2022-10-29T15:52:37Z")

</div>

By distributing tasks among several CPU cores I get about twice the performance for the critical part of the application (at least on older systems).

---

<div class="post-metadata">

**Author:** ![andreas\_g](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@andreas\_g](https://discourse.libsdl.org/u/andreas_g)\
**Post date:** [November 8, 2022, 6:53am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/49 "2022-11-08T06:53:34Z")

</div>

Still no idea what might be wrong? I might have to live with this bug, but after restructuring my app to do all rendering on the main thread another problem came up. I now have a major efficiency issue. While the emulator-thread get’s some extra performance because event handling is now on the other thread, my main thread uses way too much CPU and GPU.

I now do the rendering at 68 Hz (refresh rate of the emulated system). Before it was 60 Hz (with VSYNC from my host system). So I would expect just some minor changes. But the CPU and GPU usage almost doubled. Maybe someone can see the problem? Or someone knows a way to handle this (render screens from buffers to up to 4 windows at 68 Hz) more efficiently?

The code is here: [previous / Code / [r1221] /branches/branch\_softfloat](https://sourceforge.net/p/previous/code/HEAD/tree/branches/branch_softfloat/)

Relevant files are src/main.c, src/fast\_screen.c, src/statusbar.c and src/dimension/nd\_sdl.cpp.

---

<div class="post-metadata">

**Author:** ![Sanette](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sanette/32/2749_2.png) [@Sanette](https://discourse.libsdl.org/u/Sanette)\
**Post date:** [November 14, 2022, 9:08am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/50 "2022-11-14T09:08:52Z")

</div>

first of all, like several people said, you _really_ need to use SDL\_LockTexture() + unlock  
instead of SDL\_UpdateTexture. In my experience it is vastly faster.

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [November 14, 2022, 9:33am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/51 "2022-11-14T09:33:15Z")

</div>

> you _really_ need to use SDL\_LockTexture() + unlock  
> instead of SDL\_UpdateTexture. In my experience it is vastly faster.

Really? On which platform/render backend? I glanced at the code of several backends and SDL\_UpdateTexture() and SDL\_LockTexture()+SDL\_UnlockTexture() looked pretty similar (and sometimes one is implemented with the other, like the OpenGL SDL\_UnlockTexture() calls the SDL\_UpdateTexture() implementation)

---

<div class="post-metadata">

**Author:** ![Sanette](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sanette/32/2749_2.png) [@Sanette](https://discourse.libsdl.org/u/Sanette)\
**Post date:** [November 14, 2022, 9:48am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/52 "2022-11-14T09:48:58Z")

</div>

I did some tests a while ago on linux/opengl

but now you give me some doubts… let me check my old code

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [November 14, 2022, 10:24am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/53 "2022-11-14T10:24:29Z")

</div>

if it was a while ago, maybe the code in SDL has changed since, but currently it looks like this:

> <https://github.com/libsdl-org/SDL/blob/eef4d3c86a653f91b7221c80809ba8ab56f94cf1/src/render/opengl/SDL_render_gl.c#L847-L859>

and GL\_UpdateTexture() is what’s called by `SDL_UpdateTexture()` as well, in the common case, see

> <https://github.com/libsdl-org/SDL/blob/eef4d3c86a653f91b7221c80809ba8ab56f94cf1/src/render/SDL_render.c#L1820-L1826>

(and another case goes through `SDL_UpdateTextureNative()`, which uses `SDL_LockTexture()`, `SDL_ConvertPixels()` and `SDL_UnlockTexture()`)

---

<div class="post-metadata">

**Author:** ![Sanette](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sanette/32/2749_2.png) [@Sanette](https://discourse.libsdl.org/u/Sanette)\
**Post date:** [November 14, 2022, 10:28am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/54 "2022-11-14T10:28:21Z")

</div>

interesting. I’ll try to make some new tests. So why does the doc say “This is a fairly slow function, intended for use with static textures that do not change often.” for SDL\_UpdateTexture ?

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [November 14, 2022, 10:34am UTC](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454/55 "2022-11-14T10:34:07Z")

</div>

I have no idea why the doc says that, maybe it used to be true, or someone thought it was true and wrote it into the wiki (and then it was later synced into the doc comments in the header)?

I only glanced at the Direct3D implementations a while ago (I’m not really familiar with D3D, but know a little OpenGL), but without having compared the different functions side-by-side, IIRC that the code in the UpdateTexture() implementation roughly did the same thing as the one in LockTexture() + UnlockTexture() (with memcpy() or something in between)

[Previous page](https://discourse.libsdl.org/t/inexplainable-race-condition-in-sdl-renderer/39454.md?page=2)
