Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not quite sure what level of detail you're looking for, but here's my stab at explaining what's happening:</p> <ol> <li>A new process is created for your executable. That process has a stack segment containing each thread's stack, a data segment for static variables as well as a memory block called the heap for dynamically allocated memory, and a code segment containing the compiled code.</li> <li>Your code is loaded into the code segment, the instruction pointer is set to the first instruction in your main() method, and the code begins executing.</li> <li>Object t is allocated from the heap. The address of t is stored on the stack (each thread has it's own stack).</li> <li>t.A() is called by placing the return address to main() on the stack and changing the instruction pointer to the start of t.A()'s code. The return address is placed on the stack along with the values 1 and 2.</li> <li>t.A() calls t.C() by placing the return address to t.A() on the stack and changing the instruction pointer to the start address of t.C()'s code.</li> <li>t.C() returns by popping the return address to t.A() off of the stack and setting the instruction pointer to that value.</li> <li>t.A() returns in a similar manner to t.C().</li> <li>The call to t.B() is very similar to the call to t.A() except that it returns a value. The exact mechanism to return that value is language and platform dependent. Often the value will be returned in a CPU register.</li> </ol> <p>Note: Since your methods are very small, modern compilers will often "inline" them instead of making a classic call. Inlining means taking the code from the methods and injecting them straight into the main() method rather than going through the (slight) overhead of making a function call.</p> <p>Given your example I don't see how threading could come into the picture directly. If you were to start the executable a second time, it would run in a new process. That means it would get it's own code segment, data segment and stack segment completely isolating it from the first process.</p> <p>If your code were run inside a larger program that called main() on several threads, it would run almost exactly as previously described. The code is thread safe because it doesn't access any potentially shared resources such as static variables. There is no way that Thread 1 could "see" Thread 2 because all key data (values and pointers to objects) is stored on the thread's local stack.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload