Resources are just pointers to memory. These pointers could be the direction of an instance of a class (as much of a singleton as you want it to be), thus sharing it with whoever has a copy of the resource or another resource pointing to the same memory. Keep in mind the following:
Access to the memory should be synchronized, maybe using mutexes or condition variables.
Resources can be sent across nodes, but they are only relevant in the node that created it. If you try to derefernce a resource in another node, you'll get an error.
If a resource is left without references, it may be GC'd like any other term (you're able to provide a destructor). If a resource goes to another node and back, without saving a reference to the resource, it may have been collected. Other than that creating the singleton is "as simple" as:
Initialize the memory during NIF module loading, storing it in the module's priv data
Create resources pointing to that memory, use the resources as you want
Access the resource memory through proper synchronization methods
1条答案
按热度按时间m1m5dgzv1#
Resources are just pointers to memory. These pointers could be the direction of an instance of a class (as much of a singleton as you want it to be), thus sharing it with whoever has a copy of the resource or another resource pointing to the same memory.
Keep in mind the following:
Other than that creating the singleton is "as simple" as: