golang's Introduction
golang's People
Forkers
hsiehminchien wangyh1990 edvcc xb-devops chen00jian yuxian9542 meison92 touch-fish-small-team wcpddd beigege fastopen freedoow tropixz half-coconut xubujin0320 sunjie2021 jingjingpiggy hanhan0871 zicheng-pan 1170064770 cchenxi lhuang89 yongqiangyue cc199312 cc19931216 amy-dai siwangs xfgjdsytdsg gitjumping daviddjj rexlibing muyifang1 lynda-li licly curefun hpc-cri-uc wsws1872 sweetbreeze-wang sn0wpeak zhaoran90 thurstonzk2008 xiangxiaoc leventtsang coolgiserz 2null kingleoric2010 fly-open-k8s xiaozefeng geminijun bernylinville zsnmwy guozheng007 codeprh coderfei bdl-1989 zhouqi2016 fumengatdj xuanzhen-xu wanhk1989 yhaing guozhaoliang xiaokaidong yanghuij nengwu765 weeksunday hellobug0 ericy2020 ceidion lihuawei321 nanshaqundao lostinaurora zuyanlong gitfengri dyiting devops06 pilipupu fffffffee xuyanlinux chaoyang-ops destinyzhl mfanjie cloud-computing-cjx matt799 kimmosc2 2bon sammyluck iwinder zh634d panbuhei paulluo2021 jaclon-m shengwang-tech hornsey itxiaoke kissyoudyb halweg zhovdawei eilian-huang netspecial cyuliugolang's Issues
条件变量例子中的条件判断问题
examples/module2/condition/main.go
中的队列 Dequeue
方法中,使用 if
进行条件检查:
func (q *Queue) Dequeue() string {
q.cond.L.Lock()
defer q.cond.L.Unlock()
if len(q.queue) == 0 {
fmt.Println("no data available, wait")
q.cond.Wait()
}
...
建议改成使用 for
循环判断,避免多线程的情况下 Wait()
返回时条件已经变化:
for len(q.queue) == 0 {
fmt.Println("no data available, wait")
q.cond.Wait()
}
例子中执行 Dequeue
的 goroutine 只有一个因此没有影响,本地试了下改成3个 goroutine 时就会出问题:
❯ go run main.go
no data available, wait
putting a to queue, notify all
no data available, wait
no data available, wait
no data available, wait
putting a to queue, notify all
panic: runtime error: index out of range [0] with length 0
goroutine 20 [running]:
main.(*Queue).Dequeue(0xc0000ba000)
/home/jarvis/go/src/github.com/cncamp/golang/examples/module2/condition/main.go:52 +0x170
main.main.func2()
/home/jarvis/go/src/github.com/cncamp/golang/examples/module2/condition/main.go:29 +0x25
created by main.main
/home/jarvis/go/src/github.com/cncamp/golang/examples/module2/condition/main.go:27 +0x105
exit status 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.