SDL_RunOnMainThread(3)

πŸ’– SDL_RunOnMainThread(3) πŸ’–

okay bestie, this one lives in the SDL3 FUNCTIONS section of the manual and it's genuinely so useful, come sit with me 🫢


✨ NAME ✨

SDL_RunOnMainThread β€” call a function on the main thread during event processing. that's the whole vibe. πŸŽ€


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_init.h>

bool SDL_RunOnMainThread(SDL_MainThreadCallback callback, void *userdata, bool wait_complete);

πŸ’… DESCRIPTION πŸ’…

okay so here's the tea, and it's actually really simple:

  • if you call this on the main thread, the callback runs immediately. like, right now, no queue, no waiting, just go πŸƒβ™€οΈ
  • if you call this on another thread, the callback gets queued for execution on the main thread during event processing. so it'll happen, just on the main thread's schedule, not yours ✨
⚠️ be careful of deadlocks when you use this, fr fr. you should not have the main thread wait for the current thread while this function is being called with wait_complete set to true. that's a deadlock recipe and we do NOT do that in this house πŸ’€

🌈 FUNCTION PARAMETERS 🌈

  • callback β€” the callback to call on the main thread. this is the thing you want to happen, main-character edition πŸ‘‘
  • userdata β€” a pointer that is passed to callback. your lil goodie bag of stuff 🎁
  • wait_complete β€” true to wait for the callback to complete, false to return immediately. blocking mode or girlboss mode, your call πŸ’…

⭐ RETURN VALUE ⭐

returns true on success or false on failure β€” and bestie, if it fails, call SDL_GetError() for more information. she will tell you what went wrong, no cap πŸ”


πŸ¦‹ THREAD SAFETY πŸ¦‹

it is safe to call this function from any thread. periodt. 🌸


🧚 AVAILABILITY 🧚

this function is available since SDL 3.2.0. so if you're on something older... upgrade, babe πŸ’œ


πŸ”₯ SEE ALSO πŸ”₯


Simple Directmedia Layer 🫢 SDL 3.4.18 β€” that's the source and version right there, mwah, hope this helped bestie, go slay your main thread βœ¨πŸ’–πŸŽ€

πŸ“¦ where this page comes from