SDL_Log and Multithreading

Hello! Is it safe to use logging from multiple threads?

Hello! Is it safe to use logging from multiple threads?

At the moment, there isn’t an explicit lock, so you’re at the mercy of the platform to get this right, and it’s not clear to me that all of the platforms do.

We should probably guarantee thread safety in there, but as of 2.0.5, we don’t.

That being said, you can call SDL_LogSetOutputFunction() to supply your own logging function, and then use a mutex in there. Something like:

static SDL_Mutex *logMutex = NULL;
static SDL_LogOutputFunction realLogFn = NULL;

static void logWithLock(void *userdata, int category, SDL_LogPriority priority, const char *message)
    realLogFn(userdata, category, priority, message);

// somewhere near startup...
logMutex = SDL_CreateMutex();
void *userdata = NULL;
SDL_LogGetOutputFunction(&realLogFn, &userdata);
SDL_LogSetOutputFunction(logWithLock, userdata);
// Now SDL_Log() will do what it always does, but definitely will be thread-safe.

Like I said, we should just fix this in SDL, but that will make it safe right now.