I’m using C aerospike client library. I have to connect to the aerospike cluster several times in multi process.
So I would like to implement a singleton pattern by C.
Now I implemented the logic like below. It is possible to connect to the aerospike cluster, however, it does not make a singleton. Could you tell me how to implement a singleton pattern by C?
Do you intend to share the aerospike instance between threads or processes?
Sharing instances between processes would require implementing shared memory which can get complicated.
There are two ways to initialize a single aerospike instance which is shared between threads.
Define a global aerospike instance and initialize this instance once at startup before other threads are created. Then, reference the global aerospike instance when the other threads are created. The C client benchmarks implements this method.
If it’s not possible to initialize the global aerospike instance before creating other threads, you can apply a lock in get_instance() and return existing global instance if it was already initiailized.