So it works very similar to normal OS thread context switching, but more 
explicit?
The code will push all local state to the go routine stack, pop the state 
for the next go routine and change the stack pointer?

And this is cheaper than context switching as threads have their own 1 or 4 
mb stack allocated while go routines have a linked list for its stack 
(afaik?) ?
is that the major difference?


Den fredag 25 november 2016 kl. 17:34:24 UTC+1 skrev Ian Lance Taylor:
>
> On Fri, Nov 25, 2016 at 2:18 AM,  <roger...@gmail.com <javascript:>> 
> wrote: 
> > I have seen a lot of confusion on how go routines actually work 
> internally. 
> > Will the function execution ontop of the go routine be rewritten to a 
> statemachine with continuations in the same way as the C# async await does? 
>
> I do not know how C# async await works.  That said, goroutines are not 
> rewritten to state machines. 
>
>
> > If I have only 1 OS thread and 1000 go routines, obviously there must be 
> some sort of "magic" that makes it possible to multiplex these ontop of 
> that thread. 
> > How do the go routines store state so that they can continue from the 
> last execution point when they resume execution? 
>
> A goroutine is basically a small stack.  All the goroutine state is 
> saved on the stack.  To switch to a different goroutine, the goroutine 
> scheduler changes the stack pointer.  On the new goroutine, it looks 
> like the function call into the scheduler simply returns, and the 
> goroutine continues running. 
>
> This is a standard programming technique known as a "coroutine".  For 
> Go we used the name "goroutine" because 1) it is cute; 2) goroutines 
> have functionality not available in most coroutine libraries, such as 
> multiplexing across many OS threads and automatic use of epoll for 
> network connections. 
>
>
> > Is it safe to say that Go gives you the exact same support as the C# 
> (and others) async await while still writing code that looks sequential? 
>
> I don't know C#.  goroutines let you write code that looks sequential 
> because it actually is sequential. 
>
> Ian 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to