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
"
6
#include "
HAL/PlatformProcess.h
"
7
#include <atomic>
8
9
namespace
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`
14
class
FSpinLock
15
{
16
public
:
17
UE_NONCOPYABLE
(
FSpinLock
);
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
{
32
FPlatformProcess::Yield
();
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
}
CoreTypes.h
PlatformProcess.h
UE::FSpinLock
Definition
SpinLock.h:15
UE::FSpinLock::UE_NONCOPYABLE
UE_NONCOPYABLE(FSpinLock)
UE::FSpinLock::Unlock
void Unlock()
Definition
SpinLock.h:42
UE::FSpinLock::Lock
void Lock()
Definition
SpinLock.h:21
UE::FSpinLock::TryLock
bool TryLock()
Definition
SpinLock.h:37
UE::FSpinLock::FSpinLock
FSpinLock()=default
UE
Definition
AdvancedWidgetsModule.cpp:13
FGenericPlatformProcess::Yield
static void Yield()
Definition
GenericPlatformProcess.h:950
Engine
Source
Runtime
Core
Public
Misc
SpinLock.h
Generated by
1.9.8