dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.98k stars 4.66k forks source link

What's the difference between the lock keyword and the new.NET 9 Lock class? #102608

Open Gaoyifei1011 opened 4 months ago

Gaoyifei1011 commented 4 months ago

lock 关键字和 .NET 9 新增加的 Lock 类有什么区别? What's the difference between the lock keyword and the new.NET 9 Lock class?

While searching the official documentation for the lock keyword, I noticed that a new Lock class has been added to.NET 9, so I was curious about how the new Lock class differs from the old lock keyword, such as their usage, their performance, and so on. 在查询 lock 关键字官方文档时,我注意到 .NET 9 新增加了一个 Lock 类,所以我很好奇新增加的 Lock 类和以往的 lock 关键字有什么区别,比如它们的用法,它们的性能等等。

https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/statements/lock

image

image In the example, try to use the lock keyword to Lock an instance of the LOCK class, this example is very confusing, I did not understand, can you explain? According to the documentation, the Lock class has its own Enter and Exit methods. 在示例中尝试使用 lock 关键字锁定 Lock 类的实例,这个例子很有迷惑性,我没有看懂,能否解释一下吗?根据文档 Lock 类有自己的 Enter 方法和 Exit 方法。

dotnet-policy-service[bot] commented 4 months ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

EgorBo commented 4 months ago

You can find the answers in the original API Proposal for it here https://github.com/dotnet/runtime/issues/34812

Long story short: it allows to take locks without providing a managed object and relying on its internal mechanisms like SyncBlocks which could be slow at times (on contention), see https://github.com/dotnet/runtime/issues/34800

It also could be a baby step towards removing object's header.

Gaoyifei1011 commented 4 months ago

You can find the answers in the original API Proposal for it here #34812

Long story short: it allows to take locks without providing a managed object and relying on its internal mechanisms like SyncBlocks which could be slow at times (on contention), see #34800

It also could be a baby step towards removing object's header.

Thank you very much. I'm trying to understand

AlgorithmsAreCool commented 3 months ago

It also could be a baby step towards removing object's header.

I was surprised to read this. Looking at some docs on the object header I didn't realize how much of it was based around juggling bits for locking. As for eliminating the rest of the header, is the idea that this would just reduce object overhead?

RenderMichael commented 3 months ago

It also could be a baby step towards removing object's header.

Does that mean there’s a remote possibility that lock-ing on a non-Lock as well as the Monitor APIs, could be obsoleted in the far future?

Gaoyifei1011 commented 1 month ago

You can find the answers in the original API Proposal for it here #34812

Long story short: it allows to take locks without providing a managed object and relying on its internal mechanisms like SyncBlocks which could be slow at times (on contention), see #34800

It also could be a baby step towards removing object's header.

I notice that the System.Threading.Lock class has removed the RequiredPreviewAttribute attribute in.NET 9-Preview 6. If it does have a performance and AOT advantage over the lock keyword (which is actually syntheticsugar in System.Threading.Monitor), it is recommended to add a code optimization message prompt in the latest.NET version. Guide users to easily migrate the lock keyword code to the System.Thrading.Lock class 我注意到 System.Threading.Lock 类在 .NET 9-Preview 6 中已经移除了 RequiredPreviewAttribute 属性。如果它相比于 lock 关键字(其实是 System.Threading.Monitor 的语法糖)的确是在性能和 AOT 上有优势,建议在最新的 .NET 版本中添加一个代码优化消息提示,引导用户便捷的将 lock 关键字的代码迁移到 System.Thrading.Lock 类中