定义(原文出处):
https://www.freepascal.org/docs-html/fcl/syncobjs/tcriticalsection.html
TRtlCriticalSection 是一个结构体,在windows单元中定义;
TCriticalSection是在SyncObjs单元中实现的类,它对上面的那些临界区操作API函数进行了封装,简化并方便了在Delphi / BCB中使用。
继承关系如下:
一、定义
二、说明
TCriticalSection is a class wrapper around the low-level TRTLCriticalSection routines. It simply calls the RTL routines in the system unit for critical section support.
(TCriticalSection是低级TRTLCriticalSection例程的类包装器。它只是调用系统单元中的 RTL 例程以获得临界区支持。)
A critical section is a resource which can be owned by only 1 caller: it can be used to make sure that in a multithreaded application only 1 thread enters pieces of code protected by the critical section.
(临界区是一种只能由 1 个调用者拥有的资源:它可用于确保在多线程应用程序中只有 1 个线程进入临界区保护的代码段。)
典型用法是用以下代码保护一段代码(MySection是一个TCriticalSection实例):
// Previous code
MySection.Acquire;
Try
// Protected code
Finally
MySection.Release;
end;
// Other code.
受保护的代码一次只能由 1 个线程执行。例如,这对于多线程环境中的列表操作很有用。
三、主要的 “方法”
(1)TCriticalSection.Acquire
说明:
Acquire attempts to enter the critical section. It will suspend the calling thread if the critical section is in use by another thread, and will resume as soon as the other thread has released the critical section.
(获取进入临界区的尝试。如果临界区正在被另一个线程使用,它将挂起调用线程,并在另一个线程释放临界区后立即恢复。)
(2)TCriticalSection.Release
说明:
Release leaves the critical section. It will free the critical section so another thread waiting to enter the critical section will be awakened, and will enter the critical section. This call always returns immediately.
释放离开临界区。它将释放临界区,因此另一个等待进入临界区的线程将被唤醒,并进入临界区。此调用始终立即返回。
end!