System.SyncObjs.TSpinLock
Delphi
  TSpinLock = record
  private const
    ThreadTrackingDisabled = $80000000;
    MaxWaitingThreads = $7FFFFFFE;
    WaitingThreadMask = $7FFFFFFE;
    AnonymouslyOwned = 1;
    LockAvailable = 0;
  private
    FLock: Integer;
    function InternalTryEnter(Timeout: LongWord): 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: LongWord): 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(void);
    bool __fastcall GetIsLockedByCurrentThread(void);
    bool __fastcall GetIsThreadTrackingEnabled(void);
    void __fastcall RemoveWaiter(void);
public:
    __fastcall TSpinLock(bool EnableThreadTracking);
    void __fastcall Enter(void);
    void __fastcall Exit(bool PublishNow = true);
    bool __fastcall TryEnter(void)/* overload */;
    bool __fastcall TryEnter(unsigned Timeout)/* overload */;
#ifndef _WIN64
    bool __fastcall TryEnter(const System::Timespan::TTimeSpan &Timeout)/* overload */;
#else /* _WIN64 */
    bool __fastcall TryEnter(const System::Timespan::TTimeSpan Timeout)/* overload */;
#endif /* _WIN64 */
    __property bool IsLocked = {read=GetIsLocked};
    __property bool IsLockedByCurrentThread = {read=GetIsLockedByCurrentThread};
    __property bool IsThreadTrackingEnabled = {read=GetIsThreadTrackingEnabled};
    TSpinLock() {}
};
プロパティ
| 種類 | 可視性 | ソース | ユニット | 親 | 
|---|---|---|---|---|
| record struct | public | System.SyncObjs.pas System.SyncObjs.hpp | System.SyncObjs | System.SyncObjs | 
説明
TSpinLock のインスタンスを使用すると、マルチスレッド アプリケーションにおけるスレッドで、他の呼び出し側スレッドをブロックせずに一時的にロックを取得できるようになります。
TSpinLock を使用すると、再入可能でない簡単なスピン ロックを実装できます。 このロックは、同期オブジェクトを使って呼び出し側スレッドをブロックしません。 その代わり、CPU サイクルを少し余分に消費して、ロックが解放されるのを待ちます。
スレッドの切り替えではなく TSpinLock を使用すると、ロックされる時間が非常に短い(数 CPU サイクル)場合には処理速度が向上します。
ただし、CPU が明け渡されるので、待機スレッドは、より優先度の高いスレッドの処理をブロックしません。