Simple Question: Is threading in Godot preepmtive?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tx350z

Need to know if threading in Godot is preemptive or cooperative and it is different on different platforms?


Until now i didn’t even know cooperative threading was a thing. Thanks for asking this question.

hilfazer | 2021-03-23 11:33

Preemptive or cooperative using what viewpoint? Are you asking whether GDScript Threads are actual native operating system threads/processes or whether Godot handles the scheduling itself and actually runs all GDScript code in a single OS thread? Or do you actually want to know whether GDscript threads need to cooperate to each get runtime? Because if you’re interested in the latter, you can just answer your own question by creating and starting two threads that print something to the console every few seconds and looking whether they both print concurrently.

archeron | 2021-03-23 14:08

I’m specifically interested in knowing what model GDScript threading uses.

tx350z | 2021-03-23 14:14

:bust_in_silhouette: Reply From: Calinou

and it is different on different platforms?

Since Godot 3.3, Godot uses std::mutex and other threading primitives from the C++ standard library. Their implementation is OS-specific. Official Windows binaries are compiled with MinGW using the POSIX thread model (pthreads). Win32 threads aren’t supported by Godot when compiling with MinGW, but they are when compiling with MSVC.

Thanks for the info. In the absence of any other answer, I’ll assume cooperative multi-threading and structure my code appropriately.

tx350z | 2021-03-23 14:15