duanpei8853
2015-01-22 05:20golang sync.RWLock似乎会产生死锁?
I've been using an RWLock for reads on a mysql database, but it appears that intermittently the following code locks the programs as it waits for a unlock?
// Returns string value from key in table specified, third parameter should be set to false if it shouldn't be case senstive.
func (self *DBStore) GetString(table string, key string, vargs...interface{}) (output string) {
defer func() { fmt.Println("GETSTRING Freeing Mutex!") }()
self.mutex.RLock()
fmt.Println("GETSTRING Got Mutex!")
defer self.mutex.RUnlock()
self.Get(table, key, &output, vargs...)
return
}
// Retreive a value at key in table specified.
func (self *DBStore) Get(table string, key string, output interface{}, vargs...interface{}) (found bool) {
defer func() { fmt.Println("GET Freeing Mutex!") }()
fmt.Println("Requesting Mutex")
self.mutex.RLock()
fmt.Println("GET Got Mutex!")
defer self.mutex.RUnlock()
Now with the above, I can see that I really don't need to perform the RLock here and I can simply enough just remove it, but I was under the impression read locks shouldn't interfere with another read lock. Also it seems to be intermittent, and it usually takes me a few times of running the same thing before it reoccurs.
Output of program is:
Requesting Mutex
GET Got Mutex!
GET Freeing Mutex!
GETSTRING Got Mutex!
Requesting Mutex
and then it just sits forever, locked. What am I missing here?
Any info would be appreciated!
go version go1.4 darwin/amd64
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- 使用Golang json.NewDecoder / json.NewEncoder
- json
- 1个回答
- Golang从..到读取字节数组并显示结果
- io
- arrays
- 2个回答
- 如何在不使用sync.WaitGroup的情况下防止死锁?
- concurrency
- channel
- 2个回答
- golang time.Now().Unix()文档与实现之间的差异?
- time
- 1个回答
- Golang exec.Command程序的奇怪行为
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 1个回答
换一换