duanpei8853
duanpei8853
2015-01-22 05:20

golang sync.RWLock似乎会产生死锁?

  • mutex

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条回答

为你推荐

换一换