How are Go channels implemented?

前端 未结 2 777
孤街浪徒
孤街浪徒 2021-01-29 19:21

After (briefly) reviewing the Go language spec, effective Go, and the Go memory model, I\'m still a little unclear as to how Go channels work under the hood.

What kind o

相关标签:
2条回答
  • 2021-01-29 19:48

    Here is a good talk that describes roughly how channels are implemented:
    https://youtu.be/KBZlN0izeiY

    Talk description:

    GopherCon 2017: Kavya Joshi - Understanding Channels

    Channels provide a simple mechanism for goroutines to communicate, and a powerful construct to build sophisticated concurrency patterns. We will delve into the inner workings of channels and channel operations, including how they're supported by the runtime scheduler and memory management systems.

    0 讨论(0)
  • 2021-01-29 19:58

    The source file for channels is (from your go source code root) in /src/pkg/runtime/chan.go.

    hchan is the central data structure for a channel, with send and receive linked lists (holding a pointer to their goroutine and the data element) and a closed flag. There's a Lock embedded structure that is defined in runtime2.go and that serves as a mutex (futex) or semaphore depending on the OS. The locking implementation is in lock_futex.go (Linux/Dragonfly/Some BSD) or lock_sema.go (Windows/OSX/Plan9/Some BSD), based on the build tags.

    Channel operations are all implemented in this chan.go file, so you can see the makechan, send and receive operations, as well as the select construct, close, len and cap built-ins.

    For a great in-depth explanation on the inner workings of channels, you have to read Go channels on steroids by Dmitry Vyukov himself (Go core dev, goroutines, scheduler and channels among other things).

    0 讨论(0)
提交回复
热议问题