Detect if code is running on main thread or not

Godot Version

4.4.1

Question

I’m working on some multi-threaded code and I’m wondering if there’s a way in GDScript to detect if code is being executed on the main thread or not. Mainly I’d like to use this for debug purposes now during development to make sure code is being executed on the right thread, and to catch cases where something might be called on the wrong thread.

In other languages I’ve used there have been methods for this, for example allowing getting the current thread, and checking if this thread is the main thread, but I’ve not found a way to do this in GDScript.

Internally in the engine there seem to be some such checks however, since for example doing things that modify the scene tree are blocked if you try to do them from a separate thread.

1 Like

There is no way to check if a process is on the main thread or not. However signals and call_deferred are thread safe, so are always on the main thread. If you need to change the scene tree in another thread, if you use a signal or a call_deferred to do this then it is guaranteed to be on the main thread.

Interacting with the active scene tree is NOT thread-safe.

PS I am being easily distracted from my own game at the moment through frustration! (With myself not Godot) and although I love checking the docs, personally everything I do is on the main thread so I have little experience with this. Sorry. I just make sure all my loading and file reading is done behind a ‘loading’ page! I find it works just fine and the delays for big files is still more than acceptable.

Thanks for the reply, and I’ve read the docs and I’ve not found any such way either, this is why I’m asking here to see if there is some way I haven’t found. I know about call_deferred() being a thread safe way of scheduling a call of a function back on the main thread next frame.

However, just to be clear signals are in my experience not always called on the main thread. They’re called on the thread the signal is emitted from as far as I can tell? As in if you my_signal.emit() in code running on a separate thread this will call anything connected to that signal on the same thread. Just as if you called that function directly. This is actually one of the cases where it would be nice to be able to tell which thread code is running on when it gets called based on a signal, as that might be less straight forward to see what code gets called based on a signal emitting than a basic function call.

However, all the standard built in node signals are emitted on the main thread if that’s what you meant.

Thread has a get_id() method…

@lostminds Actually I meant all signals but it looks like you are right and I was confused. Sorry.

Yes, I thought that looked promising as well. If that was a static method (as in Thread.get_id()) I would have expected it to return the ID of the current thread. However, it seems to be a function that you call to get the ID of a specific thread (as in my_thread.get_id()). So it doesn’t seem to let you know if that’s the thread that the current code is executing on or not.

OS has .get_thread_caller_id() and .get_main_thread_id().

Great! This looks exactly like what I was looking for, just didn’t think to look for it in OS.

1 Like