From ebc7d2bed9dfde0795388bd434f16b5550d0254a Mon Sep 17 00:00:00 2001 From: Steve Halliwell Date: Sat, 7 Dec 2019 16:57:10 +1000 Subject: [PATCH] AndyHan1001 Update FungusManager.cs Use "double checked locking" algorithm to implement the singleton for "FungusManager" class, which can improve performance. --- .../Scripts/Components/FungusManager.cs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Assets/Fungus/Scripts/Components/FungusManager.cs b/Assets/Fungus/Scripts/Components/FungusManager.cs index 45ebf836..7076d4eb 100644 --- a/Assets/Fungus/Scripts/Components/FungusManager.cs +++ b/Assets/Fungus/Scripts/Components/FungusManager.cs @@ -19,9 +19,9 @@ namespace Fungus #endif public sealed class FungusManager : MonoBehaviour { - static FungusManager instance; + volatile static FungusManager instance; // The keyword "volatile" is friendly to the multi-thread. static bool applicationIsQuitting = false; - static object _lock = new object(); + readonly static object _lock = new object(); // The keyword "readonly" is friendly to the multi-thread. void Awake() { @@ -96,18 +96,23 @@ namespace Fungus return null; } - lock (_lock) + // Use "double checked locking" algorithm to implement the singleton for this "FungusManager" class, which can improve performance. + if (instance == null) { - if (instance == null) + lock (_lock) { - var go = new GameObject(); - go.name = "FungusManager"; - DontDestroyOnLoad(go); - instance = go.AddComponent(); - } + if (instance == null) + { + var go = new GameObject(); + go.name = "FungusManager"; + DontDestroyOnLoad(go); + instance = go.AddComponent(); + } - return instance; + } } + + return instance; } }