Many times we need to generate an object in thread A and then use this object in thread B. For example, thread a generates an Image object, and thread B uses this object to generate a texture. The simple thing is that thread A will first new an image object and then throw it to thread B, and thread B deletes it when it finishes using the object.
But if this object has other uses, such as I want to use this image data to synthesize some other data to generate a new texture, I don't need to recreate this object again. In addition, the new and delete this is very non-oriented, very unfriendly.
This article tries to give a new approach to the basic principle of who generates, who manages, who releases.
Thread A generates an object and passes it to thread B using and referencing the Count plus 1, and if thread C also needs this object, continue to pass the C-reference count plus 1. When thread B finishes using this object, the message queue tells thread A to run out, and thread A gets the message, minus 1 for the object reference. The message can also be a task, such as the task of simply reducing the object reference count by 1. All plus 1 minus 1 is executed in thread A, reducing the cost of mutually exclusive threads and accurately locating the object's life management.
Multi-Threading and object lifecycle management