Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton pattern - Does early binding (with static variables involved) diminish the need of mutex locks?

They say that early binding solves the problem of synchronization. I couldn't understand "how". Is this something special to Java or the same applies to C++ too?

so, with this method we actually won't require a mutex lock?

enter image description here

like image 344
Aquarius_Girl Avatar asked May 07 '12 08:05

Aquarius_Girl


1 Answers

The JVM ensures that each class is fully loaded before allowing any access to it via other threads. This means that all static variables, including uniqueInstance above, at fully instantiated before they can be accessed. This is specific to Java and means you don't need synchronization to protect the publication of the instance.

like image 100
David Harkness Avatar answered Oct 21 '22 09:10

David Harkness