shitaibin.github.io icon indicating copy to clipboard operation
shitaibin.github.io copied to clipboard

Go面试题:并发 | 大彬 LIB

Open Shitaibin opened this issue 5 years ago • 4 comments

http://lessisbetter.site/2019/05/03/go-concurrent-problems1/

Once这个once的实现有没有什么问题? 1234567891011121314151617type Once struct { m sync.Mutex done uint32}func (o *Once) Do(f func()) { if o.done == 1 { return } o.m.Lock() defer o.m.Unl

Shitaibin avatar Jun 03 '19 01:06 Shitaibin

那个单例那个是f只执行一次的,只是有可能在f第一次执行完毕之前,one就被置为1导致不可预料的结果,并不是会执行两次,他们后边又讨论了。。。博主可以去看下

1071496910 avatar Jun 18 '19 09:06 1071496910

@1071496910 多谢,已调整

Shitaibin avatar Jun 19 '19 02:06 Shitaibin

channel 1 问题答案是对的,解析的不对,实际上存在的 goroutine 是 main 以及第二个传入 ch 的 goroutine,因为值传递的原因,它是堵塞的,ticker 也会有 goroutine,但是属于系统的,runtime.NumGoroutines并不包含系统 goroutine

Bububuger avatar Apr 17 '20 06:04 Bububuger

@Bububuger channel 1 问题答案是对的,解析的不对,实际上存在的 goroutine 是 main 以及第二个传入 ch 的 goroutine,因为值传递的原因,它是堵塞的,ticker 也会有 goroutine,但是属于系统的,runtime.NumGoroutines并不包含系统 goroutine

感谢提醒,第2个goroutine实际传入的是nil的通道,造成阻塞;NumGoroutines确实不包含Ticker的通道

Shitaibin avatar Apr 19 '20 12:04 Shitaibin