UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ScopeLock.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 "Misc/NotNull.h"
9
10namespace UE
11{
12 // RAII-style scope locking of a synchronization primitive.
13 // `MutexType` is required to implement `Lock` and `Unlock` methods.
14 // Example:
15 // {
16 // TScopeLock<FCriticalSection> ScopeLock(CriticalSection);
17 // ...
18 // }
19 template<typename MutexType>
21 {
22 public:
24
25 [[nodiscard]] explicit TScopeLock(MutexType& InMutex) : Mutex(&InMutex)
26 {
27 check(Mutex);
28 Mutex->Lock();
29 }
30
32 {
33 Unlock();
34 }
35
36 void Unlock()
37 {
38 if (Mutex)
39 {
40 Mutex->Unlock();
41 Mutex = nullptr;
42 }
43 }
44
45 private:
46 MutexType* Mutex;
47 };
48
49 // RAII-style scope locking of a synchronization primitive. Same
50 // as TScopeLock except taking the lock is conditional.
51 template<typename MutexType>
53 {
54 public:
56
57 [[nodiscard]] explicit TConditionalScopeLock(MutexType& InMutex, bool bShouldLock)
58 : Mutex(bShouldLock ? &InMutex : nullptr)
59 {
60 if (bShouldLock)
61 {
62 check(Mutex);
63 Mutex->Lock();
64 }
65 }
66
68 {
69 Unlock();
70 }
71
72 void Unlock()
73 {
74 if (Mutex)
75 {
76 Mutex->Unlock();
77 Mutex = nullptr;
78 }
79 }
80
81 private:
82 MutexType* Mutex;
83 };
84
85 // RAII-style scope unlocking of a synchronization primitive.
86 // `MutexType` is required to implement `Lock` and `Unlock` methods.
87 // Example:
88 // {
89 // TScopeLock<FCriticalSection> ScopeLock(CriticalSection);
90 // for (FElementType& Element : ThreadUnsafeContainer)
91 // {
92 // TScopeUnlock<FCriticalSection> ScopeUnlock(&CriticalSection);
93 // Process(Element);
94 // }
95 // }
96 template<typename MutexType>
98 {
99 public:
101
102 [[nodiscard]] explicit TScopeUnlock(MutexType* InMutex) : Mutex(InMutex)
103 {
104 if (Mutex)
105 {
106 Mutex->Unlock();
107 }
108 }
109
111 {
112 if (Mutex)
113 {
114 Mutex->Lock();
115 }
116 }
117
118 private:
119 MutexType* Mutex;
120 };
121}
122
140class FScopeLock : private UE::TScopeLock<FCriticalSection>
141{
142public:
151
153};
154
171class FScopeUnlock : private UE::TScopeUnlock<FCriticalSection>
172{
173public:
182};
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
T TNotNull
Definition NotNull.h:307
Definition ScopeLock.h:141
FScopeLock(TNotNull< FCriticalSection * > InSyncObject)
Definition ScopeLock.h:148
Definition ScopeLock.h:172
FScopeUnlock(FCriticalSection *InSyncObject)
Definition ScopeLock.h:179
Definition ScopeLock.h:53
TConditionalScopeLock(MutexType &InMutex, bool bShouldLock)
Definition ScopeLock.h:57
void Unlock()
Definition ScopeLock.h:72
~TConditionalScopeLock()
Definition ScopeLock.h:67
UE_NONCOPYABLE(TConditionalScopeLock)
Definition ScopeLock.h:21
UE_NONCOPYABLE(TScopeLock)
TScopeLock(MutexType &InMutex)
Definition ScopeLock.h:25
~TScopeLock()
Definition ScopeLock.h:31
void Unlock()
Definition ScopeLock.h:36
Definition ScopeLock.h:98
TScopeUnlock(MutexType *InMutex)
Definition ScopeLock.h:102
UE_NONCOPYABLE(TScopeUnlock)
~TScopeUnlock()
Definition ScopeLock.h:110
Definition AdvancedWidgetsModule.cpp:13