views:

60

answers:

2

I have a singleton class, and I will compile it as a library static(lib) or dynamic(dll).

Is it guaranteed that calls to same file in a machine always refer to same and unique instance in both cases?

Edit: What if a process loads two different library and different functions from these libraries want to use instance of singleton class. Now, they are accessing same instance, am I right?

Thanks,

+4  A: 

No, different processes will not share a singleton from a common library. Different processes use different memory spaces, so each will instantiate its own singleton.

You'll need to use some sort of shared memory or interprocess-communication to share data between processes.

If a single process loads libraries A and B, and both those libraries use a singleton from library C, then they will be using the same instance.

Stephen
+2  A: 

You don't call files. You load code into your process from the file, the code contains initialisation instructions that construct your singleton. The singleton is thus unique within your process, regardless of what other code within the process uses it. The singleton is not unique from one process to another, even if they use the same shared library.

Marcelo Cantos