Multiple threads cannot create the same object, and no one is allowed to use this object until it is fully assembled. Thus, in smart code, a non-blocking design is safe.
Destruction is a little harder. But again, properly managing the life cycle of your object can ensure that the object will never be destroyed when it is likely that some threads may use it.
A generic index can help achieve this, for example.
- build an object in a specific thread
- pass shared pointers to every thread that needs access to the object (including the thread that built it, if necessary)
- the object will be destroyed when all threads release a common pointer
But, obviously, there are other valid approaches. The key is to maintain the right boundaries between the three main stages of an object’s life: construction, use and destruction. Never allow overlapping between any of these steps.
source share