SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Concurrency & Parallelism in UE4
Tips for programming with many CPU cores
Gerke Max Preussner
max.preussner@epicgames.com
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
FPlatformAtomics
• InterlockedAdd
• InterlockedCompareExchange (-Pointer)
• InterlockedDecrement (-Increment)
• InterlockedExchange (-Pointer)
64- and 128-bit overloads on supported platforms
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
// Example
class FThreadSafeCounter
{
public:
int32 Add( int32 Amount )
{
return FPlatformAtomics::InterlockedAdd(&Counter, Amount);
}
private:
volatile int32 Counter;
};
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
Critical Sections
• FCriticalSection implements synchronization object
• FScopeLock for scope level locking using a critical section
• Fast if the lock is not activated
Spin Locks
• FSpinLock can be locked and unlocked
• Sleeps or spins in a loop until unlocked
• Default sleep time is 0.1 seconds
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
Semaphores
• Like mutex with signaling mechanism
• Only implemented for Windows and hardly used
• API will probably change
• Use FEvent instead
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
FEvent
• Blocks a thread until triggered or timed out
• Frequently used to wake up worker threads
FScopedEvent
• Wraps an FEvent that blocks on scope exit
// Example for scoped events
{
FScopedEvent Event;
DoWorkOnAnotherThread(Event.Get());
// stalls here until other thread triggers Event
}
High Level Constructs
Containers
Helpers
General Thread-safety
• Most containers (TArray, TMap, etc.) are not thread-safe
• Use synchronization primitives in your own code where needed
TLockFreePointerList
• Lock free, stack based and ABA resistant
• Used by Task Graph system
TQueue
• Uses a linked list under the hood
• Lock and contention free for SPSC
• Lock free for MPSC
TDisruptor (currently not part of UE4)
• Lock free MPMC queue using a ring buffer
High Level Constructs
Containers
Helpers
FThreadSafeCounter
FThreadSingleton
• Singleton that creates an instance per thread
FMemStack
• Fast, temporary per-thread memory allocation
TLockFreeClassAllocator, TLockFreeFixedSizeAllocator
• Another fast allocator for instances of T
FThreadIdleStats
• Measures how often a thread is idle
Parallelization
Threads
Task Graph
Processes
Messaging
FRunnable
• Platform agnostic interface
• Implement Init(), Run(), Stop() and Exit() in your sub-class
• Launch with FRunnableThread::Create()
• FSingleThreadRunnable when multi-threading is disabled
FQueuedThreadPool
• Carried over from UE3 and still works the same way
• Global general purpose thread pool in GThreadPool
• Not lock free
Parallelization
Threads
Task Graph
Processes
Messaging
Game Thread
• All game code, Blueprints and UI
• UObjects are not thread-safe!
Render Thread
• Proxy objects for Materials, Primitives, etc.
Stats Thread
• Engine performance counters
Parallelization
Threads
Task Graph
Processes
Messaging
Task Based Multi-Threading
• Small units of work are pushed to available worker threads
• Tasks can have dependencies to each other
• Task Graph will figure out order of execution
Used by an increasing number of systems
• Animation evaluation
• Message dispatch and serialization in Messaging system
• Object reachability analysis in garbage collector
• Render commands in Rendering sub-system
• Various tasks in Physics sub-system
• Defer execution to a particular thread
Parallelization
Threads
Task Graph
Processes
Messaging
FPlatformProcess
• CreateProc() executes an external program
• LaunchURL() launches the default program for a URL
• IsProcRunning() checks whether a process is still running
• Plus many other utilities for process management
FMonitoredProcess
• Convenience class for launching and monitoring processes
• Event delegates for cancellation, completion and output
Parallelization
Threads
Task Graph
Processes
Messaging
Unreal Message Bus (UMB)
• Zero configuration intra- and inter-process communication
• Request-Reply and Publish-Subscribe patterns supported
• Messages are simple UStructs
Transport Plug-ins
• Seamlessly connect processes across machines
• Only implemented for UDP right now (prototype)
Upcoming Features
Critical sections & events
• Better debugging and profiling support
Task Graph
• Improvements and optimizations
UObjects
• Thread-safe construction and destruction
Parallel Rendering
• Implemented inside renderers, not on RHI API level
Messaging
• UDP v2 (“Hammer”), BLOB attachments, more robust, breakpoints
• Named Pipes and other transport plug-ins
More lock-free containers
Questions?
Documentation, Tutorials and Help at:
• AnswerHub:
• Engine Documentation:
• Official Forums:
• Community Wiki:
• YouTube Videos:
• Community IRC:
Unreal Engine 4 Roadmap
• lmgtfy.com/?q=Unreal+engine+Trello+
http://answers.unrealengine.com
http://docs.unrealengine.com
http://forums.unrealengine.com
http://wiki.unrealengine.com
http://www.youtube.com/user/UnrealDevelopmentKit
#unrealengine on FreeNode

Weitere ähnliche Inhalte

Was ist angesagt?

Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019Unity Technologies
 
Unreal Engine 4 Introduction
Unreal Engine 4 IntroductionUnreal Engine 4 Introduction
Unreal Engine 4 IntroductionSperasoft
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)Gerke Max Preussner
 
Optimizing Large Scenes in Unity
Optimizing Large Scenes in UnityOptimizing Large Scenes in Unity
Optimizing Large Scenes in UnityNoam Gat
 
Design your 3d game engine
Design your 3d game engineDesign your 3d game engine
Design your 3d game engineDaosheng Mu
 
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?내훈 정
 
Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)
Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)
Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)Johan Andersson
 
Parallel Futures of a Game Engine
Parallel Futures of a Game EngineParallel Futures of a Game Engine
Parallel Futures of a Game EngineJohan Andersson
 
Forts and Fights Scaling Performance on Unreal Engine*
Forts and Fights Scaling Performance on Unreal Engine*Forts and Fights Scaling Performance on Unreal Engine*
Forts and Fights Scaling Performance on Unreal Engine*Intel® Software
 
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술henjeon
 
Unreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkNick Pruehs
 
Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)Johan Andersson
 
Imprementation of realtime_networkgame
Imprementation of realtime_networkgameImprementation of realtime_networkgame
Imprementation of realtime_networkgameSatoshi Yamafuji
 
Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016Graham Wihlidal
 
프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법
프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법
프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법YEONG-CHEON YOU
 
How we optimized our Game - Jake & Tess' Finding Monsters Adventure
How we optimized our Game - Jake & Tess' Finding Monsters AdventureHow we optimized our Game - Jake & Tess' Finding Monsters Adventure
How we optimized our Game - Jake & Tess' Finding Monsters AdventureFelipe Lira
 
Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...
Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...
Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...Unity Technologies
 
DirectStroage프로그래밍소개
DirectStroage프로그래밍소개DirectStroage프로그래밍소개
DirectStroage프로그래밍소개YEONG-CHEON YOU
 
게임서버프로그래밍 #7 - 패킷핸들링 및 암호화
게임서버프로그래밍 #7 - 패킷핸들링 및 암호화게임서버프로그래밍 #7 - 패킷핸들링 및 암호화
게임서버프로그래밍 #7 - 패킷핸들링 및 암호화Seungmo Koo
 
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019Unity Technologies
 

Was ist angesagt? (20)

Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
 
Unreal Engine 4 Introduction
Unreal Engine 4 IntroductionUnreal Engine 4 Introduction
Unreal Engine 4 Introduction
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
 
Optimizing Large Scenes in Unity
Optimizing Large Scenes in UnityOptimizing Large Scenes in Unity
Optimizing Large Scenes in Unity
 
Design your 3d game engine
Design your 3d game engineDesign your 3d game engine
Design your 3d game engine
 
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
 
Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)
Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)
Terrain Rendering in Frostbite using Procedural Shader Splatting (Siggraph 2007)
 
Parallel Futures of a Game Engine
Parallel Futures of a Game EngineParallel Futures of a Game Engine
Parallel Futures of a Game Engine
 
Forts and Fights Scaling Performance on Unreal Engine*
Forts and Fights Scaling Performance on Unreal Engine*Forts and Fights Scaling Performance on Unreal Engine*
Forts and Fights Scaling Performance on Unreal Engine*
 
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
 
Unreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game Framework
 
Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)
 
Imprementation of realtime_networkgame
Imprementation of realtime_networkgameImprementation of realtime_networkgame
Imprementation of realtime_networkgame
 
Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016
 
프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법
프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법
프레임레이트 향상을 위한 공간분할 및 오브젝트 컬링 기법
 
How we optimized our Game - Jake & Tess' Finding Monsters Adventure
How we optimized our Game - Jake & Tess' Finding Monsters AdventureHow we optimized our Game - Jake & Tess' Finding Monsters Adventure
How we optimized our Game - Jake & Tess' Finding Monsters Adventure
 
Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...
Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...
Unite Berlin 2018 - Book of the Dead Optimizing Performance for High End Cons...
 
DirectStroage프로그래밍소개
DirectStroage프로그래밍소개DirectStroage프로그래밍소개
DirectStroage프로그래밍소개
 
게임서버프로그래밍 #7 - 패킷핸들링 및 암호화
게임서버프로그래밍 #7 - 패킷핸들링 및 암호화게임서버프로그래밍 #7 - 패킷핸들링 및 암호화
게임서버프로그래밍 #7 - 패킷핸들링 및 암호화
 
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
 

Andere mochten auch

Plug-ins & Third-Party SDKs in UE4
Plug-ins & Third-Party SDKs in UE4Plug-ins & Third-Party SDKs in UE4
Plug-ins & Third-Party SDKs in UE4Gerke Max Preussner
 
East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4
East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4
East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4Gerke Max Preussner
 
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & InfrastructureWest Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & InfrastructureGerke Max Preussner
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersEast Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersGerke Max Preussner
 
West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4
West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4
West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4Gerke Max Preussner
 
West Coast DevCon 2014: The Slate UI Framework (Part 1) - Introduction
West Coast DevCon 2014: The Slate UI Framework (Part 1) - IntroductionWest Coast DevCon 2014: The Slate UI Framework (Part 1) - Introduction
West Coast DevCon 2014: The Slate UI Framework (Part 1) - IntroductionGerke Max Preussner
 
West Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
West Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersWest Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
West Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersGerke Max Preussner
 
UE4 Twitch 2016 05-05: Unreal Message Bus Overview
UE4 Twitch 2016 05-05: Unreal Message Bus OverviewUE4 Twitch 2016 05-05: Unreal Message Bus Overview
UE4 Twitch 2016 05-05: Unreal Message Bus OverviewGerke Max Preussner
 
East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...Gerke Max Preussner
 
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...Gerke Max Preussner
 
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...Gerke Max Preussner
 
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...Gerke Max Preussner
 
West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...Gerke Max Preussner
 
GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++Gerke Max Preussner
 

Andere mochten auch (14)

Plug-ins & Third-Party SDKs in UE4
Plug-ins & Third-Party SDKs in UE4Plug-ins & Third-Party SDKs in UE4
Plug-ins & Third-Party SDKs in UE4
 
East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4
East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4
East Coast DevCon 2014: Engine Overview - A Programmer’s Glimpse at UE4
 
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & InfrastructureWest Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersEast Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
 
West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4
West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4
West Coast DevCon 2014: Engine Overview - A Programmers Glimpse at UE4
 
West Coast DevCon 2014: The Slate UI Framework (Part 1) - Introduction
West Coast DevCon 2014: The Slate UI Framework (Part 1) - IntroductionWest Coast DevCon 2014: The Slate UI Framework (Part 1) - Introduction
West Coast DevCon 2014: The Slate UI Framework (Part 1) - Introduction
 
West Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
West Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersWest Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
West Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
 
UE4 Twitch 2016 05-05: Unreal Message Bus Overview
UE4 Twitch 2016 05-05: Unreal Message Bus OverviewUE4 Twitch 2016 05-05: Unreal Message Bus Overview
UE4 Twitch 2016 05-05: Unreal Message Bus Overview
 
East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
East Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
 
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
 
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
 
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
 
West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
West Coast DevCon 2014: Extensibility in UE4 - Customizing Your Games and the...
 
GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++
 

Ähnlich wie East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programming with many CPU cores

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017OpenEBS
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfHarika Pudugosula
 
Threading Successes 03 Gamebryo
Threading Successes 03   GamebryoThreading Successes 03   Gamebryo
Threading Successes 03 Gamebryoguest40fc7cd
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CanSecWest
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsPeter Tröger
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesHelpWithAssignment.com
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxSaiDhanushM
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...Alexandre Moneger
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1Adam Dunkels
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreadingTuan Chau
 

Ähnlich wie East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programming with many CPU cores (20)

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Operating System lab
Operating System labOperating System lab
Operating System lab
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
 
Threading Successes 03 Gamebryo
Threading Successes 03   GamebryoThreading Successes 03   Gamebryo
Threading Successes 03 Gamebryo
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
 
Os lectures
Os lecturesOs lectures
Os lectures
 
Threads
ThreadsThreads
Threads
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - Processes
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptx
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
Introduction to multicore .ppt
Introduction to multicore .pptIntroduction to multicore .ppt
Introduction to multicore .ppt
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 

Kürzlich hochgeladen

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 

Kürzlich hochgeladen (20)

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 

East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programming with many CPU cores

  • 1. Concurrency & Parallelism in UE4 Tips for programming with many CPU cores Gerke Max Preussner max.preussner@epicgames.com
  • 2. Synchronization Primitives Atomics Locking Signaling Waiting FPlatformAtomics • InterlockedAdd • InterlockedCompareExchange (-Pointer) • InterlockedDecrement (-Increment) • InterlockedExchange (-Pointer) 64- and 128-bit overloads on supported platforms
  • 3. Synchronization Primitives Atomics Locking Signaling Waiting // Example class FThreadSafeCounter { public: int32 Add( int32 Amount ) { return FPlatformAtomics::InterlockedAdd(&Counter, Amount); } private: volatile int32 Counter; };
  • 4. Synchronization Primitives Atomics Locking Signaling Waiting Critical Sections • FCriticalSection implements synchronization object • FScopeLock for scope level locking using a critical section • Fast if the lock is not activated Spin Locks • FSpinLock can be locked and unlocked • Sleeps or spins in a loop until unlocked • Default sleep time is 0.1 seconds
  • 5. Synchronization Primitives Atomics Locking Signaling Waiting Semaphores • Like mutex with signaling mechanism • Only implemented for Windows and hardly used • API will probably change • Use FEvent instead
  • 6. Synchronization Primitives Atomics Locking Signaling Waiting FEvent • Blocks a thread until triggered or timed out • Frequently used to wake up worker threads FScopedEvent • Wraps an FEvent that blocks on scope exit // Example for scoped events { FScopedEvent Event; DoWorkOnAnotherThread(Event.Get()); // stalls here until other thread triggers Event }
  • 7. High Level Constructs Containers Helpers General Thread-safety • Most containers (TArray, TMap, etc.) are not thread-safe • Use synchronization primitives in your own code where needed TLockFreePointerList • Lock free, stack based and ABA resistant • Used by Task Graph system TQueue • Uses a linked list under the hood • Lock and contention free for SPSC • Lock free for MPSC TDisruptor (currently not part of UE4) • Lock free MPMC queue using a ring buffer
  • 8. High Level Constructs Containers Helpers FThreadSafeCounter FThreadSingleton • Singleton that creates an instance per thread FMemStack • Fast, temporary per-thread memory allocation TLockFreeClassAllocator, TLockFreeFixedSizeAllocator • Another fast allocator for instances of T FThreadIdleStats • Measures how often a thread is idle
  • 9. Parallelization Threads Task Graph Processes Messaging FRunnable • Platform agnostic interface • Implement Init(), Run(), Stop() and Exit() in your sub-class • Launch with FRunnableThread::Create() • FSingleThreadRunnable when multi-threading is disabled FQueuedThreadPool • Carried over from UE3 and still works the same way • Global general purpose thread pool in GThreadPool • Not lock free
  • 10. Parallelization Threads Task Graph Processes Messaging Game Thread • All game code, Blueprints and UI • UObjects are not thread-safe! Render Thread • Proxy objects for Materials, Primitives, etc. Stats Thread • Engine performance counters
  • 11. Parallelization Threads Task Graph Processes Messaging Task Based Multi-Threading • Small units of work are pushed to available worker threads • Tasks can have dependencies to each other • Task Graph will figure out order of execution Used by an increasing number of systems • Animation evaluation • Message dispatch and serialization in Messaging system • Object reachability analysis in garbage collector • Render commands in Rendering sub-system • Various tasks in Physics sub-system • Defer execution to a particular thread
  • 12.
  • 13. Parallelization Threads Task Graph Processes Messaging FPlatformProcess • CreateProc() executes an external program • LaunchURL() launches the default program for a URL • IsProcRunning() checks whether a process is still running • Plus many other utilities for process management FMonitoredProcess • Convenience class for launching and monitoring processes • Event delegates for cancellation, completion and output
  • 14. Parallelization Threads Task Graph Processes Messaging Unreal Message Bus (UMB) • Zero configuration intra- and inter-process communication • Request-Reply and Publish-Subscribe patterns supported • Messages are simple UStructs Transport Plug-ins • Seamlessly connect processes across machines • Only implemented for UDP right now (prototype)
  • 15.
  • 16. Upcoming Features Critical sections & events • Better debugging and profiling support Task Graph • Improvements and optimizations UObjects • Thread-safe construction and destruction Parallel Rendering • Implemented inside renderers, not on RHI API level Messaging • UDP v2 (“Hammer”), BLOB attachments, more robust, breakpoints • Named Pipes and other transport plug-ins More lock-free containers
  • 17. Questions? Documentation, Tutorials and Help at: • AnswerHub: • Engine Documentation: • Official Forums: • Community Wiki: • YouTube Videos: • Community IRC: Unreal Engine 4 Roadmap • lmgtfy.com/?q=Unreal+engine+Trello+ http://answers.unrealengine.com http://docs.unrealengine.com http://forums.unrealengine.com http://wiki.unrealengine.com http://www.youtube.com/user/UnrealDevelopmentKit #unrealengine on FreeNode

Hinweis der Redaktion

  1. *** Prepared and presented by Gerke Max Preussner for East Coast MiniDevCon 2014, November 12-13th Hi, my name is Max. I work as a Sr. Engine Programmer at Epic Games and would like to give you a quick overview of multi-threading capabilities in Unreal Engine 4. Games and applications that perform concurrent tasks on one or multiple CPUs or CPU cores are generally hard to write, proof correct, debug and maintain. Not only are there many concepts and mechanisms you have to learn, but also a wide range of very subtle issues that are often dependent on the operating system and the processor it is running on. Unreal Engine provides a range of features, helpers and tools to develop for multi-core computers and mobile devices more easily.
  2. At the lowest level, the Engine provides a number of synchronization primitives used to synchronize memory access across different threads. Commonly used primitives are atomic operations, mechanisms for locking and signaling threads, as well as for waiting on events from other threads. Atomic operations allow the CPU to read and write a memory location in the same indivisible bus operation. Although this seems to be a rather trivial feature, it is the foundation of many higher level multi-threading mechanisms. The main advantage of atomics is that they are very quick compared to locks, and they avoid the common problems of mutual exclusion and deadlocks. Unreal Engine exposes various atomic operations, such as incrementing and compare-and-exchange in a platform agnostic API. Most atomic operations operate on 32-bit values, but we also provide APIs for 64- and 128-bit values, if the particular platform supports them.
  3. Here is an example from our code base that uses atomics to implement a thread-safe counter. A normal increment instruction for integers may involve multiple bus operations that read the current value, increment it and write it back to memory. During these multiple steps it is possible – in principle and in practice – that the operating system suspends our thread, and another thread that also wishes to increment the counter modifies our value, causing that increment to be lost when our thread resumes. The use of ‘interlocked add’ in this example ensures that the increment operation cannot be interrupted. Because the counter value can change in asynchronous ways that the compiler cannot foresee, we use the volatile keyword to prevent optimizations of code that accesses the value.
  4. The main locking mechanisms used in Unreal Engine are Critical Sections and Spin Locks. Critical Sections provide mutually exclusive access to a single resource, usually the internals of a thread-safe class. In combination with Scope Locks they ensure that a given block of code can be executed by only one thread at a time. Compared to atomic operations, they can be a lot slower when the lock is accessed by more than one thread. Spin Locks also provide exclusive access, but instead of using the mutex facilities provided by the OS they sleep in a loop until the lock is removed. The sleep time is configurable (0.1 seconds by default), and for very small durations they may actually just do a no-op spin loop under the hood.
  5. Semaphores are similar to mutual exclusion locks, but they also contain a signaling mechanism. There is currently an implementation in FPlatformProcess, but it is not quite what one would expect, only implemented for Windows and only used in Lightmass right now. A better choice for signaling between threads would be the FEvent class…
  6. … which also uses a mutex under the hood. We frequently use events in worker threads to notify them about newly added work items or that we wish to shut them down. If you just want to send a one-shot signal from another thread, you can pass an FScopedEvent into it, and your current thread will stall when the event exits its scope until it is triggered.
  7. Most container types and classes in the Engine, including TArray and TMap are not thread-safe. We usually just manually add atomics and mutual exclusion locks in our code on an as-needed basis. Of course, locks are not always desirable, so we are also starting to implement basic lock-free containers that are thread-safe. A very useful lock-free container is TQueue, which is lock- and contention-free for SPSC and lock-free for MPSC. It is used in many places that transfer data from one thread to another. (We also implemented a lock-free MPMC queue using the Disruptor pattern at some point – it is not part of UE4 right now, but if you need it, send me an email!)
  8. We also provide some high level helper objects that are built on top of the synchronization primitives, some of which are shown here. The thread-safe counter we have already seen in the code example earlier. Thread-safe singletons are special singletons that create one instance of a class per thread instead of per process. Memory stacks allow for ultra-fast per-thread memory allocations. We also have a way to measure thread idle times that is always enabled, even in Shipping builds.
  9. Traditionally, the main work horse of multi-threaded programming are operating system threads. We expose those in the form of the FRunnable base class that you can inherit from. If your thread object inherits from FSingleThreadRunnable, it can also be used when multi-threading is disabled or unavailable. The thread pool from Unreal Engine 3 is still available as well. You can create your own thread pools or just use the global one in GThreadPool.
  10. Unreal Engine 4 uses threads for a number of things, but the most important ones are GameThread, RenderThread and StatsThread. All code that interfaces with UObjects must run on the GameThread, because UObjects are not thread-safe. The RenderThread allows us to perform rendering related tasks in parallel with the game logic. We recently also added the StatsThread that collects and processes a number of perforce counters for profiling.
  11. Threads are very heavyweight. Task Based Multi-Threading allows for more fine grained, lightweight use of multiple cores. A task is a small, parallelizable unit of work that can be scheduled to available worker threads that are managed by our Task Graph system. As the name suggests, tasks can also have dependencies on each other, so that certain tasks are not executed until other tasks completed successfully. The Task Graph is used by an increasing number of systems in the Engine, and we encourage you to use it, too.
  12. The Profiling Tools in the Editor come with a handy feature for visually debugging threads. It shows the cycle counter based statistics, as well as Task Graph usage.
  13. Unreal Engine 4 also has platform agnostic APIs for creating and monitoring external processes. You can start programs, launch web URLs or open files in other applications. If you need fine control over creating and monitoring external processes, you can use the FMonitoredProcess class.
  14. Messaging is another important architectural pattern for multi-threaded and distributed applications. Unreal Message Bus implements a message passing infrastructure that can be used to send commands, events or data to other threads, processes and even computers. Our main goal was to unify all the different network communication technologies we used for our tools in UE3, and to allow users to create their own distributed applications and tools without having to deal with low level technicalities, such as network protocols and how exactly messages get from A to B. Messages are simple Unreal structures (that currently require a tiny bit of boilerplate, but we will fix that soon). They can be sent between devices, even across the internet - if desired – and all our Unreal Frontend tools heavily rely on it. Messaging is a great way to get information out of your code classes in a loosely coupled way, and future use cases may include achievement systems, event processing for AI and real-time remote editing on game consoles and mobile phones.
  15. Debugging the flow of messages tends to be quite cumbersome in code, so the Messaging system comes with a visual debugging tool as well. It is not quite finished yet, but already functional.
  16. We are continuously working on improving and extending Unreal Engine 4. That UObjects are not thread-safe is sometimes a limitation, and we are currently working on allowing their construction and destruction on non-Game threads. We are also working on parallelizing the Renderer. This will be implemented not in the RHI API, but in the renderers themselves and utilize the Task Graph. The Messaging System will become very important for the new Editor tools we have scheduled for next year, so we will make it more powerful and easier to use as well. Much of our thread-safe code is currently lock based, and we have gotten good results with it, but we are also planning to add more generic lock-free containers.
  17. With that I would like to conclude this talk. Make sure to check out our extensive materials on the internet, all of which are available for free – even if you don’t have a subscription yet. Are there any questions I can answer right now?