FAQ
Hey,

I've just discovered go's way of dealing with concurrency. I find it neat,
but also confusing... A mix between threads and greenlets. So I'm trying to
understand how this is working under the hood.
When I'm retrieving something from a channel (<- c) inside a goroutine,
what I understand is that I'm implicitly yielding to the go scheduler, thus
allowing other goroutines (within the same thread) to run. But I don't know
how my goroutine knows when the "c" channel is ready yet.
Does the go scheduler have an "IO task" that it runs after each goroutine
yields, and has a mapping of goroutines and the channels, sockets...
they're waiting for ?
Does the scheduler work some like this ?

1) scheduler picks a goroutine G1 from goroutines stack and run it. G1
yields back to the scheduler and waits for channel c
2) scheduler maps G1 to channel c in a "waiting for IO" map.
2) pick goroutine G2 and run it. G2 yields back to scheduler
3) scheduler checks if any IO is ready. Yes, c is ready.
4) scheduler wakes up G1 to continue.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Konstantin Khomoutov at Apr 8, 2016 at 7:27 pm

    On Fri, 8 Apr 2016 07:10:28 -0700 (PDT) mehdigmira@gmail.com wrote:

    I've just discovered go's way of dealing with concurrency. I find it
    neat, but also confusing... A mix between threads and greenlets. So
    I'm trying to understand how this is working under the hood.
    [...]

    Please consider reading [1] -- it's a bit outdated but true in its
    essense and is written for mere mortals.

    1. https://morsmachine.dk/go-scheduler

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mehdigmira at Apr 8, 2016 at 8:14 pm
    Thanks for the answer. The article is great. Just to make sure I got it
    right:
    When a goroutine G1 waits for a channel, it yields back to the context. The
    context runs other goroutines in the queue, until it's G1's turn again.
    Then if the channel is ready, The goroutine can continue, else it yields
    again.
    Is this how this works ?

    Le vendredi 8 avril 2016 21:28:04 UTC+2, Konstantin Khomoutov a écrit :
    On Fri, 8 Apr 2016 07:10:28 -0700 (PDT)
    mehdi...@gmail.com <javascript:> wrote:
    I've just discovered go's way of dealing with concurrency. I find it
    neat, but also confusing... A mix between threads and greenlets. So
    I'm trying to understand how this is working under the hood.
    [...]

    Please consider reading [1] -- it's a bit outdated but true in its
    essense and is written for mere mortals.

    1. https://morsmachine.dk/go-scheduler
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Apr 9, 2016 at 1:29 am

    On Fri, Apr 8, 2016 at 1:14 PM, wrote:
    When a goroutine G1 waits for a channel, it yields back to the context. The
    context runs other goroutines in the queue, until it's G1's turn again. Then
    if the channel is ready, The goroutine can continue, else it yields again.
    Is this how this works ?
    Not really. If a goroutine waits to read on a channel, it gets added
    to a queue for that channel. When some other goroutine writes to the
    channel, it delivers the value to the first goroutine waiting on the
    queue and puts that goroutine on the queue of goroutines ready to run.
    So while a goroutine is waiting on a channel, it is not taking any
    scheduler time.

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mehdi GMIRA at Apr 9, 2016 at 6:15 pm
    Ok thanks !
    On Apr 9, 2016 3:29 AM, "Ian Lance Taylor" wrote:
    On Fri, Apr 8, 2016 at 1:14 PM, wrote:

    When a goroutine G1 waits for a channel, it yields back to the context. The
    context runs other goroutines in the queue, until it's G1's turn again. Then
    if the channel is ready, The goroutine can continue, else it yields again.
    Is this how this works ?
    Not really. If a goroutine waits to read on a channel, it gets added
    to a queue for that channel. When some other goroutine writes to the
    channel, it delivers the value to the first goroutine waiting on the
    queue and puts that goroutine on the queue of goroutines ready to run.
    So while a goroutine is waiting on a channel, it is not taking any
    scheduler time.

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 8, '16 at 3:24p
activeApr 9, '16 at 6:15p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase