What is the correct way of using multithreading in C#? Is it to use C# or Godot Threads? I first tried using C# threads to generate some meshes in the background but it crashes and I can't pinpoint the exact reason since there's no stack trace. The game just gets stuck and crashes. Then I tried starting a simple Godot thread, but the game freezes immediately without any error messages when the thread is started. This is probably because I'm not starting the thread correctly. Unfortunately I was not able to find any resources online. I'm interested in the preferred way of doing multithreading and whether multithreading in C# is even supported yet. Thanks.

edit: I experimented with C# threads and found out that they generally work. In my case calling the function 'Commit(ArrayMesh mesh)' from 'SurfaceTool' in another thread brings the game to a crash.

Thanks for the link. However my question is about whether it works in Godot. As I mentioned in my post, some functions crash when called from another thread than the main thread and there is no obvious race condition I could think of. If I have time, I will design as small example and post it here. I would be interested to hear if anybody had similar problems.

I did suggest it should work, but I defer to those who have used threading more in Godot.Perhaps provide the code that crashes.

Here is a very simple example that crashes. using Godot; using System; using System.Threading; public class Node : Godot.Node { public override void _Ready(){ System.Threading.Thread t = new System.Threading.Thread(() => this.Test()); t.Start(); } public void Test(){ var imageTexture = new ImageTexture(); // this does not work GD.Print("This is not printing because the thread crashed at the previous line. "); } }

So, creating a new ImageTexture object cannot be done from another thread. This is just one example, but I stumbled upon several things that I was not able to perform from another thread (for example using SurfaceTool.commit()). This behavior makes no sense to me. Either Godot does not support C# threads completely or they have to be used a specific way and not the way shown in my example. I would like to emphasize that multithreading indeed works when no Godot methods are called. Also many Godot methods can be called from other threads. For example I was able to generate a mesh in the background with SurfaceTool, but had to call the 'commit' method from the main thread. The example I present crashes because of the line I indicated. Without this line, the message "This is..." gets printed to the console as expected.

Here is the error when running from the console:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at (wrapper managed-to-native) Godot.NativeCalls.godot_icall_ImageTexture_Ctor(Godot.ImageTexture) at Godot.ImageTexture..ctor () [0x00019] in /home/bluepangolin55/.local/share/godot/mono/solutions/5516125489248431140_2_3/GodotSharp/ObjectType/ImageTexture.cs:54 at Node.Test () [0x00001] in /home/bluepangolin55/workspace/bugtest/Node.cs:13 at Node.<_Ready>b__0_0 () [0x00000] in /home/bluepangolin55/workspace/bugtest/Node.cs:8 at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <71d8ad678db34313b7f718a414dfcb25>:0 at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <71d8ad678db34313b7f718a414dfcb25>:0 at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <71d8ad678db34313b7f718a414dfcb25>:0 at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <71d8ad678db34313b7f718a414dfcb25>:0 at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <71d8ad678db34313b7f718a414dfcb25>:0 ================================================================= Got a SIGSEGV while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. =================================================================

Couple of suggestions. Look into using Tasks instead of direct Threads, and also can you confirm the Godot methods are threadsafe? I'll try and remember to try this when at home and not work.

4 years later