Is SDL_GetError thread safe or not?

The code for SDL_GetErrorMsg implies that it’s supposed to be thread safe to work with, but SDL_GetError calls that with a pointer into a static char buffer. And I think that’s just a global static buffer, not a thread local buffer? I’m not a C expert, so I could be totally off base.

Basically, is the error stuff thread safe? If I send my audio processing to another thread and it causes an error, will writing that error potentially cause a data race? Can I read the error from the audio thread? Or should it somehow signal to the main thread that the main thread should go check the error and just hope that no one else has caused an error in the mean time?

1 Like

(friendly bump, hope that’s okay?)

I share your concern. In my multi-threaded SDL2 app I call SDL_GetError() and rely on it returning the last error that occurred in the calling thread. If it really is using a global static buffer, which would seem undesirable to say the least, I suppose it should still be safe if only one thread ever calls SDL_GetError() which I think is the case in my app.

Huh, i think you guys are right, while SDL_GetErrorMsg is thread safe, SDL_GetError is not. And i am honestly surprised that SDL_GetErrorMsg is not a public API.

You should probably open something on bugzilla.libsdl.org if noone comes in and correts us (@icculus maybe?)

You’re right, that should be a public API.

Done!
https://hg.libsdl.org/SDL/rev/8fb0748c2cce

2 Likes

Excellent!

I guess this would be in 2.0.14 though, until then I’ll use a lock on my side around GetError calls.

That’s really helpful, thanks. But can I ask that the online documentation also be updated to reflect this change (preferably linked from the SDL_GetError page too)? A ‘public’ API that you have no way of discovering or even guessing that it might exist is no use if you haven’t seen this thread.