Multi threading in Unity

Hi

Just wondering I anyone has any resources or suggestions on best practices for threading within the Unity environment.

I’m currently starting new threads for some complex data processing, using a coroutine that checks every frame for completion. Once the job is complete the coroutine continues. Is this best practice or are there other ways to approach this?

I’m also interested in approaches to error handling on other threads. Currently I catch every error, then throw it again on the main thread, so I can see what is happening in the console. This seems messy to me.

And of course I’d welcome any other discussion around threading. It’s still pretty new to me, and I’m sure there are things I’m missing just for not knowing what to google yet.

Personally I have different ways I do it.

  1. basically what you describe. But I wrap it up in a class representation. Basically it’s a custom yieldinstruction (with my RadicalCoroutine system I can write custom yield instructions) that blocks the coroutine until the yield instruction has been signaled. This allows me to create a ‘BeginSomeAsyncOperation’ method that returns such a yieldinstruction from a coroutine.

  2. The other is if I don’t want to use coroutines. Basically I have a singleton that is created at startup called ‘GameLoopEntry’:
    GitHub - lordofduct/spacepuppy-unity-framework: This framework is considered obsolete, please see new version at:

This gives me all sorts of hooks into the update loop. Including hooking in early into the update loop, late into the update loop (tardy), and other stuff.

One of them those is to invoke a function on the next update call. You can then call from a separate thread to invoke the next time update cycle.

This allows you to just return to the main thread.

Or with a waithandle, you can then invoke to the main thread, then block your thread until your invoke action is complete, and return to operating on the separate thread.

//on a separate thread
var handle = new AutoResetEvent(false);
GameLoopEntry.InvokeNextUpdate(() => {
    //do some stuff on the main thread
    handle.Set();
});

//wait until method on main thread completes
handle.WaitOne();
2 Likes

Thanks.

I’d never even thought of using events in this fashion. Going to internalise the idea for a little while before I make dramatic code changes. But I can see some interesting things shaping up.

Now off to google wait handles. I think I grasp the concept, but implementation will take some thought.

Sounds like an interesting system you have set up but how does it return to the main thread? Also you can’t modify Unity stuff on a side thread so if you wanted to modify something, you would just pass them on as arguments or how would you do it?

In that GameLoopEntry the ‘InvokeNextUpdate’ just stores a reference to the delegate.

Then next time update is called, the delegate is called.

Just look at the link supplied to the GameLoopEntry.cs on google code.

In my example code I posted here; I show how to also block the side thread until that occurs using a wait handle.

Oh, and note, in that framework I linked I include the meta file for GameLoopEntry:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyBase/GameLoopEntry.cs.meta

I include it because GameLoopEntry has to be set as the FIRST script to run. Note it’s got an executionOrder of -32000 (the lowest value it can be).