下面代碼片段,為什么執(zhí)行到counter3000多,就不執(zhí)行了。另外如果去掉fmt.Println("counterinmain:",counter)的注釋,就可以執(zhí)行完畢。這是為什么???09-02update:是在main()中第二個for循環(huán)卡死了,就是感覺goroutines沒有執(zhí)行完或者沒有按預(yù)期執(zhí)行(預(yù)期最后counter值會為9000)。但是去掉注釋就不會卡死。另外今天在公司電腦,這個偶爾會卡死,偶爾又不會卡死,不知道為什么。packagemainimport("fmt""sync")varcounterint=0funcCount(iint,lock*sync.Mutex){lock.Lock()fmt.Println("BeforeCount()",i,counter)counter++fmt.Println("AfterCount()",i,counter)lock.Unlock()}funcmain(){lock:=&sync.Mutex{}fori:=0;i=9000{break}}}
請教一個goroutines問題
紫衣仙女
2019-04-14 11:18:33