How it works...
The shared_ptr class has an atomic reference counter inside. When you copy it, the reference counter is incremented, and when its destructor is called, the reference counter is decremented. When the count of references equals to zero, delete is called for the object pointed by shred_ptr.
Now, let's find out what's happening in the case of boost::thread (boost::bind(&process_sp1, p)). The function process_sp1 takes a parameter as a reference, so why is it not deallocated when we do get out of the while loop? The answer is simple. The functional object returned by bind() contains a copy of the shared pointer, and that means the data pointed by p won't be deallocated until the functional object is destroyed. The functional object ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access