Efficiency of coroutine and what it does?

So I found another answer http://answers.unity3d.com/questions/255647/Yield-vs-update.html

Which explained a bunch, but I’m about to pry even deeper. I watched the Intermediate Video on using Co-Routines and in the comments, he said “It’s much faster to use Yield than to re-initialize everything”, unfortunately that is everything I know about that.

So when I see a yield in a return statement (I come from C++), first, I have to get used to it (I realize there are ways to code that are better in Unity3D I should accept, so I won’t argue that). However, I want to know what this does.

In Fork(), I only know a bit, but it seems that “The process is copied, including any active registers/ram use” (quotes mean paraphrase here btw). I’m not a comp sci major, so this is what I’ve skimmed on other answer sites and a book on OS’s that I read up to threads.

So does yield copy the process? Does it need the sigaction to handle its zombies, which is why the other answer (link above) mentions gc? Also I only know about sigaction from Beej’s guide to network programming . I’m not an expert.

However, I am aware the OS has “signals” it sends and you can respond to them, probably not a Unity3D thing though.

Finally, and this might be out of scope for Unity3D forums, is what does Fork() do exactly? So if I yield, and it’s the same thing with a coroutine, does it really copy the whole process over? I realize the bulk of the program is the media, but I used to think of programs as one thing, that loops or goes wherever, but this threading thing makes me think it’s just a series of “runnable functions”, that sleep, wake up, split/fork and come back whenever, and I’m having a harder time grasping what a program is vs a process, and how this relates to whether it’s more efficient to use a coroutine or just reinitialize the variables.

And that, was how I brought this back to Unity3D discussion. However, feel free to say “For the other half, head to Stack-O and ask that there”

Here’s a good Unite 2013 talk on co-routines.

Internally they’re basically a list of class instances (one per executing co-routine) stored in a collection. Each instance is a state machine, and uses the IEnumerator interface to iterate through the states using MoveNext.

I don’t know about Fork(), but in Unity a coroutine implements a kind of background processing. Once started, a coroutine executes independently of the caller code, which’s free to go ahead and do other jobs - it’s like a parallel machine running the coroutine “at the same time”. Actually, the whole thing is done by software: when you start a coroutine, an instance of it is created and runs until a yield instruction is found, when control is returned to the caller code. In the next frame (after all Updates), Unity awakes the coroutine automatically, which continues executing until another (or the same) yield freezes it again till the next frame, and so on. It’s a cooperative system: if a coroutine enters an infinite loop due to a missing yield, Unity freezes and you must close it (and loose your work…). I don’t know exactly how they do it, but each coroutine instance has its own stack, what allows temporary variables to keep their values independently of their equivalents in other instances.

Coroutines are efficient, but allocate memory: avoid starting coroutines very often (each frame, for instance), because this will cause more frequent garbage collections and reduce performance.