Are Kotlin's singletons thread safe? - thread-safety

Are Kotlin singletons (more specifically, object declarations) thread-safe by construction? If not, what is the best practice to write thread safe singletons in Kotlin?
I would guess they are, but I haven't been able to find any explicit statement about it in the docs.

Kotlin "object" is thread-safe by construction. As you can see in any decompile/dumping tool, declared object is just final class with static instance initialization + language syntax sugar to simplify instance access

Related

Purpose of instantiation of Service classes

I have repeatedly seen a Ruby pattern of initializing objects of Service classes, i.e. classes that have a singular "call" instance method and nothing else.
For example in the dry-validation gem (https://dry-rb.org/gems/dry-validation/1.8/schemas/) the contract class is initialized, then that instance is used for validation and never used again.
Same with other Service classes, I found multiple tutorials where it is used like ServiceClass.new(stuff).call().
I don't understand why to jump through the additional hoop of object initialization, when you can achieve the same behavior by simply defining a class method that does the job, like ContractClass.call(example: "hi") and ServiceClass.call(stuff) respectively.
Am I missing something here? Repeatedly initializing one-of objects seems completely pointless to me.

singletons and google coding-style

Google c++ coding-style does not allow non-trivial static objects (and for a reason) and hence no singletons. At the same time singletons do represent reality of application logic.
So what is the correct way to implement singleton functionality google-style:
(a) have static pointers and initialize them on startup as a separate step (e.g. by linked list of initializer/maker classes)
(b) have context holding references to all singleton-like object and pass it with every method
(c) have context to be member of every class
(d) something else?
The "Google C++ Style Guide" does mention "Types representing singleton objects (Registerer)"
You can see an implementation of said registerer in ronaflx/cpp-utility with "util/registerer.h" for function pointers (illustrated here), and util/singleton.h for classic singleton.
The OP points to their own project alex4747-pub/proper_singleton.

Spring framework scope - Is it a misnomer?

I know there is a term Bean scope in Spring framework, and for some reasons it is confusing me, mainly because of the term scope, because we have this terms (scope) in languages, like C, Java applied to a variable scope (that is where a variable is visible).
I know there are five bean scope, and I am not asking explanation on them, I am not clear on what Bean scope means. Can anyone help me understand what does this term mean?
In programming languages scope of the variables defines where in the code, the variable can be reached.
Global variables can be accesed from everywhere.
Function parameters or local variables can be accesed only in the function.
In Spring framework scope of the bean defines when during the application runtime we are dealing with the same object.
Singleton scoped bean is an object that is unique to the whole application. Like global variable in programming languages.
Session scoped bean is an object that is unique to the session.
Request scoped bean is an object that is unique to the request. Like function parameters.
Prototype scoped bean is not unique to anything. Every time you get it, you have a new copy. Hard to compare but it can be a heap allocated variable.
In computer science, the scope term is overloaded, the same as the term interface. You can have a Go or Java interface as well as PCI or ISA.
Well, the term overload is also overloaded.

Can a class have its own method outside of an interface under DI?

I have been reading about DI and autowiring some objects in Spring and came across this situation - Classes A and B implement interface I. Class C gets an autowired dependency of type I - i.e. an object of A or B. Inteface I has methods m1 and m2 which are implemented by both A and B - which C can now invoke. What if, A has its own method m3 and B has its own method m4 - m3 and m4 are not part of I. I am unable to allow C to invoke these. Is there a workaround this? If not, is there a design principle which is against this?
Thanks
I am not sure if I understand the problem since there is no sample code. Here is my answer from what I do understand. I don't think you can invoke a method of an instance that is injected into your object via an interface unless the method is in the contract. It beats the whole purpose of using interfaces. You can only use what is defined in the interface. If your class implementing that interface have additional methods, the only way I can think of calling them is via some reflection magic. But then, that would be going against patterns and best practices.

LoadLibrary Static/Globals and Threads

Say I have a DLL that has the following static/global:
ClassA Object;
Along with the implementation of ClassA, it also contains a 'regular' ClassB, which will not work properly if ClassA has not been constructed yet (which is why I've made ClassA is a static/global).
In Windows, I believe that the DLL loader will load this DLL on the call to ClassB's constructor, correct? At this point, ClassA will be constructed and then ClassB's construction will follow. If a second thread comes along and constructs ClassB, ClassA will not be constructed as it has already been constructed.
Now, my question is -- what if ClassB is constructed simultaneously by two threads. So Thread 1 will start to construct ClassA. Will Thread 2 wait until ClassA is completely constructed before executing ClassB's constructor?
In other words, does LoadLibrary() use a CriticalSection to ensure thread-safe initialization of a DLL's static/globals? My hunch is 'yes', but I can't seem to find any documentation saying one way or the other.
DllMain is called by the Windows loader while holding an internal critical section known as the "loader lock," so your static constructors will be called during the DLL_PROCESS_ATTACH event, which only occurs once, when your DLL is first loaded.
Look at the documentation for DllMain; I believe it talks about the loader lock and initialization order.
DLL's are not initialized like EXE's since they are shared by multiple processes. What you need is effectively a singleton object that is a one time factory for your other objects.
Note, I'm assuming here by "ClassA" and "ClassB" you mean instances of those classes...
For example you could have a someting like
ClassA& GetTheClassAInstance();
ClassB& GetTheClassBInstsance();
The first time these are called, these functions would ensure that your global instances of ClassA and ClassB were properly constructed.

Resources