• Noir
    2021-12-19
    package main import "fmt" import "time" func main() { chArr := [4]chan struct{} { make(chan struct{}), make(chan struct{}), make(chan struct{}), make(chan struct{}), } for i := 0; i < 4; i++ { go func(i int) { for { <- chArr[i % 4] fmt.Printf("i am %d\n", i) time.Sleep(1 * time.Second) chArr[(i + 1) % 4] <- struct{}{} } }(i) } chArr[0] <- struct{}{} select{} }

    作者回复: 对

    共 4 条评论
    9
  • 星星之火
    2020-12-05
    channel 中包含的 mutex 是什么呢,和课程最开始的 sync.mutex 是同一个东西吗? 因为 sync.mutex 是依赖 channel 实现的,感觉应该不是同一个 mutex?

    作者回复: 不是同一个,只是类似。channel中这个是运行时内部使用的mutex

    共 3 条评论
    4
  • Geek_43dc82
    2022-04-19
    我实在是太蠢了,只能写出这样的代码了 package main import "fmt" func main() { signChan1 := make(chan struct{}) signChan2 := make(chan struct{}) signChan3 := make(chan struct{}) signChan4 := make(chan struct{}) mainSignChan := make(chan struct{}) for i := 1; i <= 4; i++ { go func(i int) { for { select { case <-signChan1: fmt.Println(1) signChan2 <- struct{}{} case <-signChan2: fmt.Println(2) signChan3 <- struct{}{} case <-signChan3: fmt.Println(3) signChan4 <- struct{}{} case <-signChan4: fmt.Println(4) signChan1 <- struct{}{} } } }(i) } signChan1 <- struct{}{} <-mainSignChan }
    展开

    作者回复: 写的不错,简单直接

    共 3 条评论
    2
  • 陌
    2021-04-27
    Goroutine 泄漏的那个例子,如果把 unbuffered chan 改成容量为 1 的 buffered chan,那么假如函数超时了,子 goroutine 也能够往 channel 中发送数据。那么 GC 会把这个 channel 回收吗?

    作者回复: 会

    
    2
  • 老猫
    2022-01-23
    func chgoroutine(in,out,stop chan struct{},n int) { for{ select{ case <-in: fmt.Println(n) time.Sleep(time.Second) out <-struct{}{} case <-stop: return } } } func main() { ch1 := make(chan struct{}, 0) ch2 := make(chan struct{},0) ch3 := make(chan struct{},0) ch4 := make(chan struct{},0) stop := make(chan struct{},0) go chgoroutine(ch1,ch2,stop,1) go chgoroutine(ch2,ch3,stop,2) go chgoroutine(ch3,ch4,stop,3) go chgoroutine(ch4,ch1,stop,4) ch1 <-struct{}{} time.Sleep(time.Second * 20) stop <-struct{}{} }

    作者回复: 😃

    
    1
  • chimission
    2023-01-07 来自北京
    package main import ( "fmt" "time" ) func printChan(c chan int) { st := <-c fmt.Println(st%4 + 1) time.Sleep(1 * time.Second) c <- st + 1 go printChan(c) } func main() { ch := make(chan int, 4) ch <- 0 printChan(ch) select {} }

    作者回复: 基本上,你这个写法还是串行的实现

    
    
  • 清风
    2022-10-19 来自北京
    func main() { chArr := []chan struct{}{ make(chan struct{}), make(chan struct{}), make(chan struct{}), make(chan struct{}), make(chan struct{}), make(chan struct{}), } for k, _ := range chArr { if k == len(chArr)-1 { go goon(chArr[k], chArr[0], k+1) } else { go goon(chArr[k], chArr[k+1], k+1) } } chArr[0] <- struct{}{} select {} } func goon(ch chan struct{}, ch2 chan struct{}, index int) { time.Sleep(time.Duration(index*10) * time.Millisecond) for { <-ch fmt.Printf("I am No %d Goroutine\n", index) time.Sleep(time.Second) ch2 <- struct{}{} } }

    作者回复: OK

    
    
  • 张觥
    2022-10-18 来自北京
    func TestChannel1Practice(t *testing.T) { var ch = make(chan struct{}) wg := sync.WaitGroup{} wg.Add(4) go func() { ch <- struct{}{} }() for thread := 1; thread <= 4; thread++ { go func(thead int) { _, ok := <-ch if ok { for i := 1; i <= 4; i++ { t.Logf("%d: %d", thead, i) time.Sleep(1 * time.Second) } wg.Done() ch <- struct{}{} } }(thread) } wg.Wait() t.Log("finished") }

    作者回复: 你这样并不是每个goroutine打印自己的id

    
    
  • 草色青青
    2022-09-18 来自北京
    func tt(ctx context.Context, c1, c2 *chan int) { for { select { case n := <-*c1: fmt.Println(n) nn := n + 1 if n == 4 { nn = 1 } *c2 <- nn //fmt.Printf("c1:%p,c2:%p\n", c1, c2) case <-ctx.Done(): return } } } func PrintInfo() { ctx, cancel := context.WithCancel(context.Background()) c1, c2, c3, c4 := make(chan int, 2), make(chan int, 2), make(chan int, 2), make(chan int, 2) fmt.Printf("c1:%p,c2:%p,c3:%p,c4:%p\n", &c1, &c2, &c3, &c4) go tt(ctx, &c1, &c2) go tt(ctx, &c2, &c3) go tt(ctx, &c3, &c4) go tt(ctx, &c4, &c1) c1 <- 1 fmt.Println("Hello, 世界") time.Sleep(time.Millisecond * 70) cancel() fmt.Println("Hello, 世界") }
    展开

    作者回复: 👍🏻

    
    
  • Penn
    2021-12-22
    // 4个goroutine ch1 := make(chan struct{}) ch2 := make(chan struct{}) ch3 := make(chan struct{}) ch4 := make(chan struct{}) go func() { for { <-ch1 fmt.Println("1") time.Sleep(time.Second) ch2 <- struct{}{} // <-ch1 } }() go func() { for { <-ch2 fmt.Println("2") time.Sleep(time.Second) ch3 <- struct{}{} } }() go func() { for { <-ch3 fmt.Println("3") time.Sleep(time.Second) ch4 <- struct{}{} } }() go func() { for { <-ch4 fmt.Println("4") time.Sleep(time.Second) ch1 <- struct{}{} } }() ch1 <- struct{}{} select {}

    作者回复: 可以。代码有重复,还可以优化下

    
    