Unity Multithreading Problem

Hi there.
Please have a look at the following script:

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
		Debug.Log("First Thread:" + Thread.CurrentThread.ManagedThreadId.ToString());
		
		Thread newThread = new Thread(RunThread1);
		newThread.Start();	
	}
	
	public void RunThread1()
	{
		Debug.Log("Second Thread: " + Thread.CurrentThread.ManagedThreadId.ToString());		
	}
}

When I attach it to a gameobject and press play in UnityEditor. I get the following output as expected:

  • First Thread: 1
  • Second Thread: 2

If I stop playmode and enter playmode again, I get the following output:

  • First Thread: 1
  • Second Thread: 1

So it seems, that in Unity Editor, only the first time I enter the playmode a new thread is created?
On my machine this happens all the time (at least for all tests I could made).
Am I missing something or is this a bug?

I’m using unity 3.5.1 on Windows7 64bit.

Thanks for any help.

You can not interact with unity functions from your own thread.

You could use none monobehvaiour inheriting classes to do thread work, as long as you don’t call any unity functionality within it.

Unity is not thread safe. It uses a main thread and some threading for its own usage such as WWW and skinmesh processing.
You could use the coroutines to simulate threading functionality (unity uses coroutines to do that - http://docs.unity3d.com/Manual/Coroutines.html).