+ 1

Is singletone really a singletone across projects?

Hi There are three projects of Visual studio as below: 1. P1: This project is MFC exe and this exe has one function as F1. This F1 function calls a function F2 from another project P2 2. P2: This is DLL project which has function F2 which eventually gets called from P1 project 3. P3: This is a DLL and it has just an implementation of a singletone class. Now, I am including singletone class of P3 project into P1 and P2. In other words, I am creating class object in function F1 of P1 project and function F2 of P2 project. F1 function calls F2 and hence both objects (singleton class object) are not created at a specific same point of time. Query is that does F2 function execution ensures that I will get same Singleton object created in F1 or it will create new object ? Many thanks in advance... feel free to ask in case of any queries.

11th Sep 2021, 9:48 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Antworten
+ 2
In order to make a Singleton thread save you need to create a lock/padlock which is a static wrapper class arround your initializer.
14th Sep 2021, 5:03 AM
jack
jack - avatar
+ 1
As it is a single programm split into three modules it will create only a single instance of the singelton, that is if the implementation is done correctly. Assuming that is the case, due to the static accessor and the private constructor there shouldn't be a second instance unless explicitly coded so. You can create a test where you return the instances hash code on "creation". It should be the same hashcode each time.
12th Sep 2021, 3:08 PM
jack
jack - avatar
+ 1
Thanks jack ... I have implemented singleton without being into different modules... Like to make it thread safe , we have to implement and consider about two threads reaching same time.... I am looking for something which tells that you have to take care of this and that if code is in three different modules... Is there something or no extra precautions is required when it is splitted
12th Sep 2021, 6:17 PM
Ketan Lalcheta
Ketan Lalcheta - avatar