TCriticalSection 定义说明

本文介绍了TCriticalSection类,它是Delphi/BCB中用于简化临界区操作的封装。TCriticalSection通过调用系统单元中的RTL例程来提供临界区支持,确保多线程环境中代码段的互斥访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义(原文出处):
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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值