‹ BackHN Continuity

Thread

Go Concurrency Distilled

402 points · 187 comments · chmaynard

  1. SamInTheShell · · focus · HN ↗
    The concurrency and threading in Go just feels like magic compared to every other language. I'm a goroutine addict and I refuse to be rehabilitated.

    Just from observations over the years, I don't think there's any other language quite like this, in terms of how things can end up happening in any thread.

    1. osigurdson · · focus · HN ↗
      >> in terms of how things can end up happening in any thread

      Doesn't that describe pretty much any green thread style concurrency implementation.

      1. dlisboa · · focus · HN ↗
        No. Preemptive scheduling plus M:N mapping combination that Go has is not common in other major implementations.

        Other languages and their implementations of green threads usually have cooperative scheduling or M:1 mapping

        1. seabrookmx · · focus · HN ↗
          C# and Rust (via Tokio) both have M:N threading. They both use a work-stealing algorithm to map many tasks onto a finite thread pool. But you're correct that they are cooperative via async/await, not pre-emptive.
          1. serbuvlad · · focus · HN ↗
            Very curious how much Go wins by this. How worse would typical Go programs run with pre-emption disabled?
            1. foldr · · focus · HN ↗
              I believe it’s more about robustness than performance in typical cases. Without pre-emption, there’s always a risk of one goroutine using disproportionate CPU time if it gets into an infinite (or just very long) loop without doing any IO.
            2. ncruces · · focus · HN ↗
              You can try this yourself: GODEBUG=asyncpreemptoff=1

              Also platforms like Wasm still do Mx1 scheduling without async preemption, where Gosched is required at places.

              E.g.: my "transpiled" SQLite driver takes special care to make sure long running SQL queries (and the busy handler) can be canceled with contexts even on platforms without async preemption.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.