# Doubt about code's flow

**URL:** https://discussions.unity.com/t/doubt-about-codes-flow/423973
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 26, 2010, 11:55pm UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973 "2010-09-26T23:55:07Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 26, 2010, 11:55pm UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/1 "2010-09-26T23:55:07Z")

</div>

Hello, i was reading the documentation and i am confused now.

It says:

function teste() {  
a();  
b();  
}

The b function call could be finished before a ends first, how can it be possible? Does unity creates one thread for each function to run in parallel? I was reading that i can work around this using coroutines, but i would like to know how the stuffs works behind the curtains

Cheers.

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [September 26, 2010, 11:57pm UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/2 "2010-09-26T23:57:35Z")

</div>

> [@anon\_66055356](#):
>
> The b function call could be finished before a ends first, how can it be possible?

It’s possible if it’s a coroutine.

> [@](#):
>
> Does unity creates one thread for each function to run in parallel?

No.

> [@](#):
>
> i would like to know how the stuffs works behind the curtains

[http://unity3d.com/support/documentation/ScriptReference/index.Coroutines\_26\_Yield.html](http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html)

–Eric

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 12:06am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/3 "2010-09-27T00:06:16Z")

</div>

Thanks for the replay, Eric 🙂

Just one more question 🙂

Do ();  
print (“This is printed immediately”);

function Do ()  
{  
print(“Do now”);

yield WaitForSeconds (2);

print(“Do 2 seconds later”);  
}

You told me that it’s possible if it is a coroutine, but in the example above  
print (“This is printed immediately”);  
Is printed before the  
print(“Do 2 seconds later”);  
executes, but Do() is not a coroutine. I did not understand yet, sorry for my ignorance;

Cheers

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 12:27am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/4 "2010-09-27T00:27:30Z")

</div>

I read that in javascript every call to a function the compiler creates a coroutine, so Do() actually is a coroutine, so I think that I did not understand the coroutine itself. How the code is divided by the processor when a coroutine is called 🙂

Thanks Eric

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [September 27, 2010, 12:58am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/5 "2010-09-27T00:58:16Z")

</div>

> [@anon\_66055356](#):
>
> I read that in javascript every call to a function the compiler creates a coroutine

A function is only called as a coroutine if it actually is a coroutine. If it has “yield” in it somewhere, then it is. You can write out “StartCoroutine” if you want, but in Unityscript it’s not necessary.

> [@](#):
>
> How the code is divided by the processor when a coroutine is called

It’s not divided, it’s all in one thread.

–Eric

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 1:00am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/6 "2010-09-27T01:00:37Z")

</div>

Hello again, I was reading about the coroutines, so I understood the concept (i guess), coroutines give me a mechanism to order and save the state using first-class continuations. I already played with setjmp and longjmp in c++, but i can trace the execution going to a function and returning to it caller. In Unity calling Do(); the executing goes to Do() method, if i evaluate a yield, it will return and if i call Do() again the flow will continue from the yield I have called. So i can see the execution flow, but in the example Do() is called and print (“This is printed immediately”); is printed before Do() finished. I cannot figure out what happened, is the true flow behavior created during compilation to .Net dlls?

Cheers.

---

<div class="post-metadata">

### Author: ![appels](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/appels/32/495192_2.png) [@appels](https://discussions.unity.com/u/appels)
#### Post date: [September 27, 2010, 1:02am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/7 "2010-09-27T01:02:23Z")

</div>

if you call the function Do() from the function Start() in Unity, it will only get called once.  
It will start the coroutine DO() and execute it immediately

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 1:05am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/8 "2010-09-27T01:05:46Z")

</div>

Sorry for my english Eric, I would like to know the flow that Unity does when calling the functions, when one is called and returned, started and finished, can you teach me that?

Thanks so much

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 1:10am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/9 "2010-09-27T01:10:57Z")

</div>

Thanks Appels, I know Start is called only once and before an update call and it is usually used to innitialize some stuffs, my doubt is how the flow is managed by the unity, i cannot see the returning from one to call another and how the two can be managed in parallel without multhread, so if it’s sequential how unity organized the stack calls to each coroutine

---

<div class="post-metadata">

### Author: ![Dreamora](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dreamora/32/91058_2.png) [@Dreamora](https://discussions.unity.com/u/Dreamora)
#### Post date: [September 27, 2010, 1:15am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/10 "2010-09-27T01:15:32Z")

</div>

through a discrete tasklet scheduler.

Whenever you call Startcoroutine (or just yield in unityscript) or yield, your function with a pointer to this point within the code is put back onto the future execution handling (likely priority queue as it needs to be sorted by the distance from now to the future to efficientely process the stuff in the right order for relationship management etc) where it will be executed once that point in the future is either reached or has passed (the execution has no millisecond precision granularity)

if you have some LUA background: It knows a similar concept of Coroutine. Going by that, you can think of “yield” as a call that splits your function internally at compile into two functions and that makes the part before the function “coroutine call” into the part with yield and after it which then directly goes to sleep with yield.

thats at least my understanding from experience of how it works and how such things are done even for real threads on OS level

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [September 27, 2010, 1:17am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/11 "2010-09-27T01:17:08Z")

</div>

> [@anon\_66055356](#):
>
> So i can see the execution flow, but in the example Do() is called and print (“This is printed immediately”); is printed before Do() finished. I cannot figure out what happened, is the true flow behavior created during compilation to .Net dlls?

No, if it did that then it would be impossible to test in the editor. If you want it to wait for the coroutine to finish, then use

```auto
yield Do();

```

instead of

```auto
Do();

```

–Eric

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 1:23am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/12 "2010-09-27T01:23:36Z")

</div>

Thanks Dreamora, it is what i was looking for, cause everybody understand the concept but not the internal behavior.  
Thanks Eric.

Cheers.

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 1:24am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/13 "2010-09-27T01:24:17Z")

</div>

Studying unity to my university project, so i need to know some engine mechanisms. 🙂

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 4:44am UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/14 "2010-09-27T04:44:13Z")

</div>

Hello again, if somebody would become interested in how the unity keep track of the coroutines created, cause nobody knows or can’t tell me what kind of architecture is used to managed the scheduler of the coroutines inside Unity.  
I found this article that can give you an idea about that: (I said idea)

> **[Coroutines with IEnumerable and yield return](http://incrediblejourneysintotheknown.blogspot.com/2008/04/coroutines-with-ienumerable-and-yield.html)**
>
> There was a brief flurry of excitement about the fancy yield return syntax when the C# 2.0 specification was first published some years a...

And how yield can works in C #:  
[http://www.ytechie.com/2009/02/using-c-yield-for-readability-and-performance.html](http://www.ytechie.com/2009/02/using-c-yield-for-readability-and-performance.html)

An Unity architect reading this could light my way or keep the secret locked by seven keys… 😛

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 4:13pm UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/15 "2010-09-27T16:13:15Z")

</div>

var global = 0;

Example ();  
print (global);

function Example ()  
{  
print (global);  
yield WaitForSeconds (2);  
global = 1;  
print (global);  
}

This wil print  
#=\> 0  
#=\> 0  
#=\> 1

When the flow reachs yield WaitForSeconds (2); the flow is back to the caller and the next time that Example() is called the flow goes to  
global = 1; print (global);  
I would like to know who called Example again, cause i don’t have this in the code, How the flow is managed inside Unity to organized the code cause it is not parallel, the wiki gives a illusion mentioning this word, but if there is only the main thread and I don’t have the knowlodge about the sequential instruction I will be tricked managing global variables as in my example where the values to print could change cause i don’t kow which code is done first.  
Can someone give me an explanation?

Cheers.

PS: Again sorry for my English \>\<

---

<div class="post-metadata">

### Author: ![anon\_94124338](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@anon\_94124338](https://discussions.unity.com/u/anon_94124338)
#### Post date: [September 27, 2010, 6:12pm UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/16 "2010-09-27T18:12:21Z")

</div>

The numbers on left represents the sequence of execution.

1. var global = 0;

2. Example ();

3. print (global);

function Example ()  
{  
3. print (global);  
4. yield WaitForSeconds (2);  
6. global = 1;  
7. print (global);  
}

when yield WaitForSeconds(2) is called, the funtion halts its execution then the code continue running from where the function Exemple was called.  
After 2 seconds have passed, the execution continues at number 6 (global = 1).

Probably some scheduling algorithm take care of looking when and from where any suspended functions need to continue its execution.

---

<div class="post-metadata">

### Author: ![anon\_66055356](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_66055356](https://discussions.unity.com/u/anon_66055356)
#### Post date: [September 27, 2010, 8:06pm UTC](https://discussions.unity.com/t/doubt-about-codes-flow/423973/17 "2010-09-27T20:06:08Z")

</div>

Obrigado Felipe, deve a ver alguma forma de organização de código que a Common Language Runtime utiliza para gerenciar a troca de contextos e execuções das funções. Estou procurando algum documento que me leve mais a fundo, agora nada que seja a respeito com a Unity.

Abraço.
