On Fri, Nov 25, 2016 at 2:18 AM, <rogerals...@gmail.com> 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.