dsh7623 2018-07-16 11:05
浏览 33
已采纳

去例行公事

I am leaning go, so this might be a stupid question.

I can't seem to figure out why one of my go routines is being blocked by another. My understanding (could be wrong) go routines run as independent light weight threads so they should not be blocking each other, unless I have messed up :)

I have pasted the code below and would appreciate any help / tips to figure this out.

package main
import "fmt"
import "time"
import "sync"



func worker( jobs <-chan int, job2 chan<- int) {
    defer wg.Done()
    for j := range jobs {
        fmt.Println("finished job", j)
        time.Sleep(time.Second/2)

        if(j%3==0){
           job2 <- j   
        }  

   } 
   close(job2)
   fmt.Println("channel job2 closed")
 }

func worker2(job2 <-chan int) {
    defer wg.Done()
    for i:= range job2 {
        fmt.Println(i)
        time.Sleep(time.Second*10)
    } 
}

var wg sync.WaitGroup

func main() {

    wg.Add(2)
    jobs := make(chan int)

    job2 := make(chan int)

    go func() {
        for j := 1; j <= 10; j++ {
             jobs <- j
        }
        close(jobs)
        fmt.Println("channel jobs closed")
    }()

    go worker(jobs,job2)
    go worker2(job2)

    wg.Wait()
    fmt.Println("exiting main")     

}

I get the following output when I run this code

finished job 1
finished job 2
finished job 3
finished job 4
3
finished job 5
finished job 6
6
finished job 7
finished job 8
finished job 9
9
finished job 10
channel jobs closed
channel job2 closed
exiting main

however I was expecting something like this?

finished job 1
finished job 2
finished job 3
finished job 4
3
finished job 5
finished job 6
finished job 7
finished job 8
finished job 9
finished job 10
channel jobs closed
6
9    
channel job2 closed
exiting main
  • 写回答

1条回答 默认 最新

  • dongzhuifeng1843 2018-07-16 11:30
    关注

    Your routines are sort-of blocking because the channels aren't buffered. A write/read on an unbuffered channel is a blocking operation. Therefore your routines by definition have to wait on eachother.

    Essentially, your sleep of half a second is kind of irrelevant, because the second worker sleeps for 10 seconds. Those 10 seconds will block reads/writes to the second channel. Add a buffer to the channel to get around this.

    Some other things I'd like to point out are:

    • time.Sleep(time.Second/2) isn't going to work (well, it is, but dividing by 3 for example isn't). time.Sleep expects a time.Duration argument, which is an int64. You need to pass something like time.Millisecond * 500 instead
    • It's bad form to pass a channel to a routine, and close it from the routine that didn't create the channel. The channels creation and closing should be contained within a single routine. If not, it works, but maintenance becomes a real nightmare.
    • Group your imports, instead of repeating the import "package", just use import ( "package1" "package2")
    • Don't use globals if you don't have to. Create the waitgroup in the function that starts all of the routines, and pass a pointer to it to all routines. Including the anonymous function, just to be safe (once you start adding buffers to channels, for example)
    • Consider looking into context.Context and the select construct. You can create a context.WithCancel and in select listen for ctx.Done() in all routines. Then you can just cancel all routines in one go without having to handle signals and pushing stuff onto a cancel channel

    Demo

    I've changed a couple of things (mainly channel creation, and some minor code-cleanup), and created a playground example here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算