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

Delphi TCriticalSection Acquire vs Enter - what is the difference?

我正在更新 Delphi (Delphi 2009) 代码,它只使用 TCriticalSection.Acquire/Release 对,而不是 Enter/Release or 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. Call Acquire to block all other threads from acquiring this critical section until the Release or Leave method is called. Acquire does the same thing as the Enter method.

Enter: Blocks other threads when the calling thread enters a thread-sensitive section. Call Enter to block all other threads from entering code protected by this critical section until the Leave or Release method is called. Enter calls the Acquire method to bind the critical section to the calling thread.

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

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

的调用而实现的

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

的范围