x


Is the Unity API threadsafe?

I notice that Unity supports background loading of asset bundles, so I guess uses threads. Can I use threads in my own script and plugins? Are there any issues to be aware of?

more ▼

asked Oct 07, 2009 at 04:53 PM

Graham Dunnett gravatar image

Graham Dunnett ♦♦
24.1k 33 49 155

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

No.

Every call you make into the Unity API, must be done from your main thread. It is possible for you to create your own threads, to make whatever calculations you like, however, the additional threads you create yourself should not call any Unity functions. They should communicate their results back to the mainthread, which can then call the Unity functions.

If you try to do this anyway, it might accidentally work, however future Unity updates (including those of the webplayer) are very likely to make this break.

more ▼

answered Oct 18, 2009 at 01:42 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
9k 32 80 128

Hey Keli, thanks for that information (and also thanks to whoever implemented that) - sounds like a great idea to me to make Unity more robust!

Dec 10, 2009 at 06:06 PM jashan

the assert you get is:

m_ThreadCheck && !Thread::EqualsCurrentThreadID(m_ThreadID)

Jan 12, 2010 at 08:50 PM Graham Dunnett ♦♦

I have been running into this issue, without explicitly starting a new thread. The one thing I suspect is the root of this evil is my usage of a static class, my lazy way of keeping track of my "globals". When trying to asyncloadlevel from my static class i get the dreaded m_ThreadCheck message

Mar 29, 2010 at 07:04 AM user-1883 (google)

Dislike. ;)

Feb 24, 2011 at 03:59 AM yoyo

I have a crash to desktop and the error m_ThreadCheck && Thread::mainThreadId && !Thread::CurrentThreadIsMainThread() but I've no idea how to prevent this or how I managed to create multiple threads. Details of my issue are posted at http://answers.unity3d.com/questions/373556/how-can-i-stop-my-editor-from-crashing-everytime-i.html

Jan 03, 2013 at 01:55 PM grimmy
(comments are locked)
10|3000 characters needed characters left

You can achieve some of the advantages of threads by using coroutines - see the Coroutines and Yield overview in the Scripting Reference.

more ▼

answered Nov 07, 2009 at 09:48 PM

technicat gravatar image

technicat
1.7k 16 18 54

this question is asked and answered by unity technologies staff to teach us something. someone that wants to use threads already knows coroutines. :D threads are some of the most advanced stuff in programming. however using them in C# is alot easier than C++. unity's scripting engine is the best choice that they made! thank you guys

Dec 31, 2009 at 03:21 PM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9503
x98
x3

asked: Oct 07, 2009 at 04:53 PM

Seen: 8921 times

Last Updated: Jan 04, 2013 at 01:04 PM