Browse Source

AndyHan1001 Update FungusManager.cs

Use "double checked locking" algorithm to implement the singleton for "FungusManager" class, which can improve performance.
master
Steve Halliwell 5 years ago
parent
commit
ebc7d2bed9
  1. 11
      Assets/Fungus/Scripts/Components/FungusManager.cs

11
Assets/Fungus/Scripts/Components/FungusManager.cs

@ -19,9 +19,9 @@ namespace Fungus
#endif #endif
public sealed class FungusManager : MonoBehaviour 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 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() void Awake()
{ {
@ -96,6 +96,9 @@ namespace Fungus
return null; return null;
} }
// Use "double checked locking" algorithm to implement the singleton for this "FungusManager" class, which can improve performance.
if (instance == null)
{
lock (_lock) lock (_lock)
{ {
if (instance == null) if (instance == null)
@ -106,9 +109,11 @@ namespace Fungus
instance = go.AddComponent<FungusManager>(); instance = go.AddComponent<FungusManager>();
} }
return instance;
} }
} }
return instance;
}
} }
#endregion #endregion

Loading…
Cancel
Save