- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Go1.18 新特性:TryLock 是什么?需要它吗?由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
大家好,我是 polarisxu.
我们知晓,Go 标准库的 sync/Mutex、RWMutex 实现了 sync/Locker 接口, 提供了 Lock() 和 UnLock() 方法,可以获取锁和释放锁,我们可以方便的使用它来控制对共享资源的并发控制。(其他语言,比如 Java 是有类似 TryLock 的功能的) 。
但是锁被获取后,在未释放之前其他 goroutine 再调用 Lock 则会被阻塞住,这种设计在有些情况下可能不能满足需求。有时我们希望尝试获取锁,如果获取到了则继续执行,如果获取不到,我们也不想阻塞住,而是去调用其它的逻辑,这个时候我们就想要 TryLock 方法:即尝试获取锁,获取不到也不堵塞.
这个需求,2013 年就有人提出,但官方没有采纳。2018 年又有人提出:https://github.com/golang/go/issues/27544,建议增加 TryLock,但没有下文。直到 2021 年 4 月,有人再次提出,同时也给出了标准库中需要的场景:https://github.com/golang/go/issues/45435.
不过,Go Team 的负责人 rsc 提出了反对的意见:
Locks are for protecting invariants. If the lock is held by someone else, there is nothing you can say about the invariant. 。
TryLock encourages imprecise thinking about locks; it encourages making assumptions about the invariants that may or may not be true. That ends up being its own source of races. 。
There are definitely locking issues in http2. Adding TryLock would let us paper over them to some extent, but even that would not be a real fix. It would be more like the better your 4-wheel-drive the farther out you get stuck. 。
I don't believe http2 makes a compelling case for TryLock. 。
他认为 TryLock 会鼓励设计者对锁进行不精确的思考,这可能最终会成为 race(竞态) 的根源。同时,他认为仅为 http2 提供 TryLock 不值得,希望有更具说服力的案例.
然后大家进行了一些讨论,同时 rsc 给了一个实现,并提到:
sync: add Mutex.TryLock, RWMutex.TryLock, RWMutex.TryRLock 。
Use of these functions is almost (but not) always a bad idea. 。
Very rarely they are necessary, and third-party implementations (using a mutex and an atomic word, say) cannot integrate as well with the race detector as implmentations in package sync itself. 。
也就是现在 Go1.18 中实现的三个方法。不过,rsc 建议,大家尽量别使用它.
可见,最后 rsc 妥协了,因为有人提出了一些实现 TryLock 的代码。就像 neild 说的,虽然大部分时候可能确实不需要 TryLock,但出现各种第三方版本的 TryLock,并非好事,而应该有一个官方的实现.
看看 Mutex.TryLock 官方的实现:https://pkg.go.dev/sync@master#Mutex.TryLock,强调虽然存在正确使用 TryLock 的情况,但很少见。可见官方是勉为其难的添加了它.
关于 TryLock,2017 年鸟窝大佬写过一篇文章, 如何自己实现一个,而且对比了几种实现方式的性能,感兴趣的可以阅读:https://colobu.com/2017/03/09/implement-TryLock-in-Go/.
本文转载自微信公众号「polarisxu」,可以通过以下二维码关注。转载本文请联系polarisxu公众号.
原文链接:https://mp.weixin.qq.com/s/NGiBcw0Je-clQd0paAsI9Q 。
最后此篇关于Go1.18 新特性:TryLock 是什么?需要它吗?的文章就讲到这里了,如果你想了解更多关于Go1.18 新特性:TryLock 是什么?需要它吗?的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
目录 C#特性 1. 概括 2. 语法 定义特性类 应用特性
是否有关于 Python 语言必须提供哪些功能的文章/论文?为什么应该使用 Python 而不是任何其他语言? Python 的优点和缺点是什么? 最佳答案 Why Python和 Why Pytho
我想弄清楚为什么 .x比*.x具有更高的特异性当后者有望获胜时。 不是*.x应该具有 0-0-1-1 的特异性(1 个类,1 个标签)而 .x只是一个类(class) 0-0-1-0 ? 考虑以下基本
在尝试评估非 instanceof 条件时,我发现了我在 Groovy 2.4.7、1.6.0 JVM 中没有预料到的行为。 总之: class Foo { static Boolean
当使用可变结构和属性时,编译器可以解决一些问题,但不能对其他相当明显的事情做同样的事情,这对我来说似乎很奇怪。 以下面的自动属性为例: Vector2 Vector { get; set; } 而不是
我对 ES3 有一定的了解,但我对 ES5 的特性不是很了解。我 - 或多或少 - 知道: Object.create(), Object.freeze() “使用严格” getter 和 sette
我最近开始将 Django 1.5.4 用于带有 MySQL 后端的 Web 应用程序。就在一开始,我遇到了某些限制,这让我想知道 Django 是否是继续前进的正确方法。 一些明显的缺点是: 缺少复
在函数模板的定义中,模板参数的实例化一般是未知的。类型特征可用于在编译时获取一些信息。例如,这是 is_pointer 的一个简单应用: template void foo(T p) { cout
我正在设计页面的样式,该页面具有除最后一个框外的带底部边框的连续框。我为所有框应用类 .box 并添加 .box_last 以仅隐藏最后一个框的边框。 .box { border-bottom-s
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我有兴趣编制一份不建议在嵌入式系统中使用的 c++ 功能列表(这可能会导致人们推荐使用 c 而不是 c++)。如果您知道,请尝试添加原因,或者将您的原因添加到其他人的答案中。 这是一个开始(我知道的唯
作为一名 Ruby 程序员,您是否曾觉得任何使用起来有点冒险的功能,可能是因为它的奇怪行为?它可能有很好的文档记录,但在调试时很难找到,或者难以记住? 我通常尽量远离 String#gsub!。文档说
我正在开发一个连接到健身 watch 的蓝牙应用程序。这是我第一次使用蓝牙。我设法使用出色的 FlutterBlue 将我的应用程序与设备连接起来图书馆。 但是我无法理解我从阅读中获得的结果。这是我阅
1. 迭代器(Iterator)的介绍 背景:指针可以用来遍历存储空间连续的数据结构,但是对于存储空间费连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历。 定义:迭代器是一种检
嗨,伙计们,有没有动态更改 Spring 属性文件内容的好例子?如果您能给我一些示例或链接,我将不胜感激。 非常感谢 最佳答案 我想你可以使用 ReloadableResourceBundleMess
SystemVerilog 引入了一些非常有用的结构来改进编码风格。然而,正如我的一位同事经常说的,“你不是在写软件,你是在描述硬件。”考虑到这一点,当最终结果需要合成时,应该避免语言的哪些特征?这个
我定义了这些测试依赖项 / Test Dependencies lazy val wiremock = "com.github.tomakehurst" % "wir
我正在为 Android 手机和 Android watch (wearOS) 编写应用程序。这些应用程序将通过蓝牙相互通信。基本上,Android 手机上的应用程序将与 WearOS 设备绑定(bi
我正在为 Android 手机和 Android watch (wearOS) 编写应用程序。这些应用程序将通过蓝牙相互通信。基本上,Android 手机上的应用程序将与 WearOS 设备绑定(bi
我有兴趣提高我的设计能力(设计具有属性、方法等的类)。即如何决定类、方法和属性应该是什么? 你们能建议我改进这个的好 Material 吗? 最佳答案 请看: Any source of good o
我是一名优秀的程序员,十分优秀!