Go为什么不支持可重入锁呢?

先来看看什么是可重入锁,以下是维基百科的定义。

计算机科学中,可重入互斥锁(英语:reentrant mutex)是互斥锁的一种,同一线程对其多次加锁不会产生死锁。可重入互斥锁也称递归互斥锁(英语:recursive mutex)或递归锁(英语:recursive lock)。

如果对已经上锁的普通互斥锁进行“加锁”操作,其结果要么失败,要么会阻塞至解锁。而如果换作可重入互斥锁,当且仅当尝试加锁的线程就是持有该锁的线程时,类似的加锁操作就会成功。可重入互斥锁一般都会记录被加锁的次数,只有执行相同次数的解锁操作才会真正解锁。

递归互斥锁解决了普通互斥锁不可重入的问题:如果函数先持有锁,然后执行回调,但回调的内容是调用它自己,就会产生死锁[1]

Go里面是不支持可重入互斥锁的。看代码:

var mu sync.Mutex
func main() {
	mu.Lock()
	mu.Lock()
}

运行后会报

fatal error: all goroutines are asleep - deadlock!

Go 设计原则

在工程中使用互斥的根本原因是:为了保护不变量,也可以用于保护内、外部的不变量。

基于此,Go 在互斥锁设计上会遵守这几个原则。如下:

  • 在调用 mutex.Lock 方法时,要保证这些变量的不变性保持,不会在后续的过程中被破坏。
  • 在调用 mu.Unlock 方法时,要保证:
    • 程序不再需要依赖那些不变量。
    • 如果程序在互斥锁加锁期间破坏了它们,则需要确保已经恢复了它们。

不支持的原因

讲了 Go 自己的设计原则后,那为什么不支持可重入呢?

其实 Russ Cox 于 2010 年在《Experimenting with GO》就给出了答复,认为递归(又称:重入)互斥是个坏主意,这个设计并不好。

总结

Go 互斥锁没有支持可重入锁的设计,这样就不用关心递归层级等复杂的流程了。加锁解锁中间做了什么一目了然。

This entry was posted in Go and tagged . Bookmark the permalink.

10,027 Responses

  1. After my vasectomy, a couple years down the line, I had a build of anxiety and would have difficulty maintaining cialis on line Express delivery with tracking 7-10 business days 49 shipping cost or free delivery for orders over 180; Standard shipping 10-18 business days 39 shipping cost or free delivery for packages over 130

  2. How long does it take to feel the effects of Cialis cialis for sale Rarely, an eye problem called nonarteritic anterior ischemic optic neuropathy NAION has been reported in patients who took Cialis Soft Tabs

  3. After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  4. cialis reviews This process is highly effective, yet incredibly gentle, so that it will not cause harmful side effects, mask the personality, or negatively interact with other medications or supplements

  5. ноггано армию слушать яак йола новый год минус alexanow.ru охота на козу зимой видео мужские анекдоты про девушек

  6. Definitely believe that which you stated. Your favorite reason seemed to be on the net the easiest thing to be aware
    of. I say to you, I certainly get irked while people consider worries that they just don’t know about.
    You managed to hit the nail upon the top as well as
    defined out the whole thing without having side-effects , people can take a signal.

    Will likely be back to get more. Thanks

  7. you’re really a just right webmaster. The site loading velocity is amazing.
    It seems that you are doing any distinctive trick.

    Also, The contents are masterwork. you have performed a wonderful task
    in this matter!

  8. Hi, Neat post. There is an issue together with your website in web explorer, could check this… IE still is the marketplace leader and a big component to other folks will miss your magnificent writing because of this problem.

  9. Please let me know if you’re looking for a article writer for your blog. You have some really great posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog in exchange for a link back to mine. Please send me an e-mail if interested. Many thanks!