gpt4 book ai didi

Delphi TCriticalSection Acquire vs Enter - 有什么区别?

转载 作者:行者123 更新时间:2023-12-05 01:10:23 28 4
gpt4 key购买 nike

我正在更新 Delphi (Delphi 2009) 代码,它专门使用 TCriticalSection.Acquire/Release 对,而不是 Enter/Release 或 Leave 对。我的问题是 - AcquireEnter 有什么区别?

Delphi 文档非常晦涩——它甚至试图说没有区别:

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/!!MEMBERTYPE_Methods_SyncObjs_TCriticalSection.html

Acquire: Binds the critical section to the calling thread. CallAcquire to block all other threads from acquiring this criticalsection until the Release or Leave method is called. Acquire does thesame thing as the Enter method.

Enter: Blocks other threads when the calling thread enters athread-sensitive section. Call Enter to block all other threads fromentering code protected by this critical section until the Leave orRelease method is called. Enter calls the Acquire method to bind thecritical section to the calling thread.

我想要方法 TryAcquire,但没有这样的方法,所以 - 我正在考虑用 TryEnter 替换我对 Acquire 的所有调用... Sleep... 循环,受 TryEnter 调用次数的限制。但是要确定会发生什么,我应该知道 AcquireEnter 之间的区别?这个区别是什么?为什么有两种不同的方法?

最佳答案

对于 TCriticalSection 没有区别。 Enter 被实现为对Acquire 的调用。 Leave 也是如此,它被实现为对 Release 的调用。

TryEnter 方法是在 Delphi 2009 之后添加的。但它只是对 Windows API 调用 TryEnterCriticalSection 的简单包装。您可以自己直接调用该函数。例如,您可以使用类助手将 TryEnter 引入 TCriticalSection 的范围。

关于Delphi TCriticalSection Acquire vs Enter - 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64063532/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com