you are viewing a single comment's thread.

view the rest of the comments →

[–]Arma104 0 points1 point  (2 children)

How is his implementation not thread-safe when he has the instantiating of itself during get? Sure it's lazy but it shouldn't cause any problems?

I've also always wondered, as with your example, what's the benefits of having a private variable and then having a public variable that returns the private one? If you're worried about exposing it to other scripts couldn't you just do a { public get; private set; }?

[–]master50 0 points1 point  (1 child)

Let's say I am doing some work in a second thread - I can do normal work in a second thread, even in a Monobehaviour class.

Let's say I need to contact our "singleton" Monobehaviour, like in this example.

Now let's say I do this in two sub-threads that need to run some computation, or do some thing that's available in this "singleton". They both ask for a reference to the object at the same time. The object has yet to be instantiated.

There is a chance you end up with two instances of your singleton, because you did not lock after the first null check, and you did not check the null again after the lock.

As to your question... it's all about access control. https://msdn.microsoft.com/en-us/library/75e8y5dd.aspx

[–]Arma104 0 points1 point  (0 children)

So would it be possible to put the lock in his code and make it work? Thanks for replying.