Invoking main thread

I’ve seen alot of posts on how the Unity3d engine isn’t thread safe. However very few posts on how you can safely use multi-threading within Unity3d. If the engine wasn’t designed for multi-threading adding the current check is the only right thing to do. However that doesn’t mean threading isn’t supported! Or isn’t safe. It just means that Unity3d won’t try to handle all the weird race conditions that threading involves.

I’m currently making a small MMO (what an oxymoron I know!) never the less I feel I’ve narrowed the project enough that its not a monomental achievement. Blog with project outline

Now let me make it clear. I’m not using multi-threading cause its cool. Or because I have huge calculations. I use it cause most actions needs to swing by a server to be validated and/or randomized! I’m using the ASP.Net client generated by mono’s wsdl and it has two modes, blocking and async. I’m going for async and that means I get my replies in a different thread.

First I looked for methods to invoke back to main thread, but found none. Even more so I found several posts saying I’m a dummy cause I insist on using threads. :slight_smile:

Well luckily I previously did an advanced CommandQueue system. It works a bit like co-routines, it takes a bit longer to define you commands, however its slightly better suited for scripted animations and such. But the main advantage ofcourse is that it allows me to add commands in a thread-safe way and then execute them in main thread.

So my two cents to all with a similar problem. Make an abstract object with an abstract run() method. Then derive an object from that which holds the needed context information (perhaps a delegate to invoke) add it to a thread-safe list. You can then run through the list on Update() and voila!

Alternate/Simpler solutions:

Since I’m new with Unity3d I hope someone can point me towards the new feature suggestions page. Cause an InvokedCoroutine that calls the coroutine on main-thread would certainly solve most user problems.

Since I’m new with Unity3d I hope
someone can point me towards the new
feature suggestions page

Only question I could find :slight_smile:

http://feedback.unity3d.com/forums/15792-unity

Invoking the main thread is indeed a peculiar way to call what you are up to. Here is my two cents in the best hope that this helps you.

So whatever the layout of your threading may be, I will focus on the point where you need to synch anything that calls Unity-API with the main thread. Coroutines aren’t really a seperate thread, they are executed on the main thread just as well. But you can yield their execution and wait for the end of the current frame (WaitForEndOfFrame). As I see it that’s the place where you should synch. The way I would go is to have a script that starts a coroutine which yields for the end of a frame and invoke a delegate. this delegate could be filled with actions/task, whatever you will call it, by seperate threads. This way you can have an asynchronous thread that handles stuff that needs to be authorized by the server and you feed the results back in the game loop by adding some methods to the delegate.

This is not a complete system, but I hope it gets you started.

I accidentally wrote the answer in the question.

So my two cents to all with a similar problem. Make an abstract object with an abstract run() method. Then derive an object from that which holds the needed context information (perhaps a delegate to invoke) add it to a thread-safe list. You can then run through the list on Update() and voila!

Alternate/Simpler solutions:

  • PragmaScript suggests a list of actions. Word of warning, his sample keeps the list locked while the actions are executing. When a project grows big enough someone will cause a deadlock on that.

  • GabrielH suggests adding coroutines to a list and then starting the coroutine from main-thread.

  • If you just want a quick fix take PragmaScript’s solution cause it has source code.Though I’d recommend swapping the list with an empty one. Then you can release the lock fast such reducing the chances of a thread halting and waiting.

Hey guys,

I created a simple class for this reason that you can call with a one-liner.

You can use it like this:

public IEnumerator ThisWillBeExecutedOnTheMainThread() {
    Debug.Log ("This is executed from the main thread");
    yield return null;
}
public void ExampleMainThreadCall() {
    UnityMainThreadDispatcher.Instance().Enqueue(ThisWillBeExecutedOnTheMainThread());
}

Simply head over to GitHub - PimDeWitte/UnityMainThreadDispatcher: A simple, thread-safe way of executing actions (Such as UI manipulations) on the Unity Main Thread and start using it if you’d like.