Unreal Engine has an alternative implementation of std::mutex called FCriticalSection which makes it possible for your data to be safely accessed from different threads, preventing race conditions. This takes the same approach, handling it with one thread locking until the other thread completes the necessary operations.

There are two ways to handle lock/unlock logic.

The first is low-level, which is used to directly lock and unlock threads. Use with caution due to possible deadlocks . Just use FCriticalSection::Lock and FCriticalSection::Unlock functions where needed to follow this way.

Click to see full example

This is an example of thread-safe editing of a StringToEdit using the AnyClass::SetString function.

class AnyClass
{
	FString StringToEdit;

	// Marked as mutable as it's often warranted by design
	mutable FCriticalSection DataGuard;

public:
	void SetString(const FString& NewString)
	{
		DataGuard.Lock();
		StringToEdit = NewString;
		DataGuard.Unlock();
	}
};

The second is to use RAII technique , which is much safer since it automatically handles guarding within the specific scope. This is an alternative to std::lock_guard from the standard library. You can use FScopeLock to handle it.

Click to see full example

This is an example of thread-safe editing of a StringToEdit using the AnyClass::SetString function.

class AnyClass
{
	FString StringToEdit;

	// Marked as mutable as it's often warranted by design
	mutable FCriticalSection DataGuard;

public:
    void SetString(const FString& NewString)
	{
		FScopeLock Lock(&DataGuard);
		StringToEdit = NewString;
	}
};