Critical Sections

The interface "ICriticalSection" encapsulates the critical section APIs, which are mostly used to protect resources in a multi threaded application. To see a full list of the methods look at the ICriticalSection Reference.

type ICriticalSection = interface (IBasic) ['{82546200-8D73-11D3-A52E-00005A180D69}'];

Creating a new critical section is easy:

function NewCriticalSection : ICriticalSection;

Use the methods "Enter/Leave" to enter/leave a critical section (see APIs "Enter/LeaveCriticalSection"). If the critical section is already locked by another thread, entering the section means that your thread will go in a wait state until the other thread leaves the section. You can also use the method "TryEnter" (see API "TryEnterCriticalSection"), which is however not available in win95.

procedure ICriticalSection.Enter;
function  ICriticalSection.TryEnter : boolean;

function  ICriticalSection.Leave : boolean;

The following method returns whether this section is owned by the current thread:

function ICriticalSection.IsOwnedByCurrentThread : boolean;

The method "OwnerThread" returns the ID of the thread that currently owns this critical section. The method "LockCount" returns how often the owning thread has entered the session in the moment. If the critical section is not by the current thread, these informations can change at any time, namely in that moment when another thread enters or leaves the critical section.

function ICriticalSection.OwnerThread : cardinal;
function ICriticalSection.LockCount   : integer;