System.SyncObjs.TSpinLock

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

  TSpinLock = record
  private const
    ThreadTrackingDisabled = $80000000;
    MaxWaitingThreads = $7FFFFFFE;
    WaitingThreadMask = $7FFFFFFE;
    AnonymouslyOwned = 1;
    LockAvailable = 0;
  private
    FLock: Integer;
    function InternalTryEnter(Timeout: Cardinal): Boolean;
    function GetIsLocked: Boolean;
    function GetIsLockedByCurrentThread: Boolean;
    function GetIsThreadTrackingEnabled: Boolean;
    procedure RemoveWaiter;
  public
    constructor Create(EnableThreadTracking: Boolean);
    procedure Enter; inline;
    procedure Exit(PublishNow: Boolean = True);
    function TryEnter: Boolean; overload; inline;
    function TryEnter(Timeout: Cardinal): Boolean; overload;
    function TryEnter(const Timeout: TTimeSpan): Boolean; overload;
    property IsLocked: Boolean read GetIsLocked;
    property IsLockedByCurrentThread: Boolean read GetIsLockedByCurrentThread;
    property IsThreadTrackingEnabled: Boolean read GetIsThreadTrackingEnabled;
  end;

C++

struct DECLSPEC_DRECORD TSpinLock
{
private:
    static const unsigned ThreadTrackingDisabled = unsigned(0x80000000);
    static const int MaxWaitingThreads = int(0x7ffffffe);
    static const int WaitingThreadMask = int(0x7ffffffe);
    static const System::Int8 AnonymouslyOwned = System::Int8(0x1);
    static const System::Int8 LockAvailable = System::Int8(0x0);
    int FLock;
    bool __fastcall InternalTryEnter(unsigned Timeout);
    bool __fastcall GetIsLocked();
    bool __fastcall GetIsLockedByCurrentThread();
    bool __fastcall GetIsThreadTrackingEnabled();
    void __fastcall RemoveWaiter();
public:
    __fastcall TSpinLock(bool EnableThreadTracking);
    void __fastcall Enter();
    void __fastcall Exit(bool PublishNow = true);
    bool __fastcall TryEnter()/* overload */;
    bool __fastcall TryEnter(unsigned Timeout)/* overload */;
    bool __fastcall TryEnter(const System::Timespan::TTimeSpan &Timeout)/* overload */;
    __property bool IsLocked = {read=GetIsLocked};
    __property bool IsLockedByCurrentThread = {read=GetIsLockedByCurrentThread};
    __property bool IsThreadTrackingEnabled = {read=GetIsThreadTrackingEnabled};
    TSpinLock() {}
};

Properties

Type Visibility Source Unit Parent
record
struct
public
System.SyncObjs.pas
System.SyncObjs.hpp
System.SyncObjs System.SyncObjs

Description

Instances of TSpinLock allow a thread in a multithreaded application to temporarily acquire a lock while not blocking other calling threads.

Use TSpinLock to implement a simple non-reentrant spin lock. This lock does not block calling threads using a synchronization object. Instead it burns a few extra CPU cycles and waits for the lock to be released.

Using TSpinLock instead of a thread switching method increases the speed of processing if the time the lock is held is very short (few CPU cycles).

However, because the CPU is yielded, the waiting threads will not block higher priority threads from being processed.

See Also