- Home /
new Thread vs StartCoroutine
Is new Thread(functionName); basically the same as StartCoroutine(functionName) ?
Answer by Eric5h5 · Jul 07, 2012 at 05:01 AM
No, coroutines aren't threads; by default all code in Unity runs in the same thread.
yup this is correct.... new Thread() starts a function in a different thread...where as startcoroutine does nothin special and runs it in the same thread..
you could note that monodevelop cant open the file from where debug.log is called, if it is in new thread...meaning unity is single threaded by default
No idea - it is certainly correct. Anyone who feels they want to down vote Eric had probably better question their understanding of Unity. +1
Well, I am occasionally wrong. ;) In which case downvotes are fine. But indeed in this case I wasn't wrong.
what if you don't call subroutines with yield... StartCoroutine(FunctionX) ... it seems it just "thread off" as the next command is issued?
Answer by whydoidoit · Jul 08, 2012 at 07:35 PM
What happens with a coroutine is that it is run on the main thread every frame and executes until it does a yield - then it will suspend until the yield condition is met. That yield condition is tested every frame and when the condition is met the code in your coroutine resumes. A thread effectively runs at the same time as other code (this is only true when you have multiple cores). Unity cannot support accessing its API from anything apart from the main thread.
Hmm, I am still trying to understand access of Unity API apart from the main thread (I guess that is what Eric means by Unity being all on the same thread). I seem able to access Unity API methods such as GetPixels32() and others from the "other thread" (i.e., invoked in a function called from new Thread)
You can't use GetPixels32 in another thread. If you try you'll just get an error at runtime.
Your answer

Follow this Question
Related Questions
C# WaitForSeconds doesn't seem to work ?? 2 Answers
Thread ending prematurely? 2 Answers
Fire and forget coroutines 1 Answer
yield return on WWW never coming back? 0 Answers