UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SpinLock.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7#include <atomic>
8
9namespace UE
10{
11 // A mutex that doesn't put the thread into a WAIT state but instead repeatedly tries to aquire the lock.
12 // WARNING: Should be used only for very short locks
13 // Use with `TScopeLock`
15 {
16 public:
18
19 FSpinLock() = default;
20
21 void Lock()
22 {
23 while (true)
24 {
25 if (!bFlag.exchange(true, std::memory_order_acquire))
26 {
27 break;
28 }
29
30 while (bFlag.load(std::memory_order_relaxed))
31 {
33 }
34 }
35 }
36
37 bool TryLock()
38 {
39 return !bFlag.exchange(true, std::memory_order_acquire);
40 }
41
42 void Unlock()
43 {
44 bFlag.store(false, std::memory_order_release);
45 }
46
47 private:
48 std::atomic<bool> bFlag{ false };
49 };
50}
Definition SpinLock.h:15
UE_NONCOPYABLE(FSpinLock)
void Unlock()
Definition SpinLock.h:42
void Lock()
Definition SpinLock.h:21
bool TryLock()
Definition SpinLock.h:37
FSpinLock()=default
Definition AdvancedWidgetsModule.cpp:13
static void Yield()
Definition GenericPlatformProcess.h:950