Performance Improvement Function shared_ptr use_count

My problem with the application is as follows:

I have a big foo structure. Since they are large and for memory management, we do not want to delete them when data processing is completed.

We save them in std::vector<boost::shared_ptr<foo>>.

My question is about understanding when all the processing is complete. The first solution is that we don’t want any other application code to put a complete flag in the structure, because the program has several execution paths, and we cannot predict which one is the last.

So, in our implementation, as soon as the processing is completed, we will delete all copies boost::shared_ptr<foo>>, except the one in the vector. This will reduce the reference counter in shared_ptr to 1. Is it practical to use shared_ptr.use_count () to make sure it is 1, to know when all the other parts of my application are done with data.

Another reason I ask the question is because the acceleration documentation for the shared pointer shared_ptr recommends not using "use_count" for production code.


Edit - I did not say that when we need a new foo, we will scan the vector of pointers foo that are looking for foo that is not currently in use, and use this foo for the next round of processing. This is why I thought having a reference counter 1 would be a safe way to ensure that this particular foo object is no longer used.

+3
source share
5 answers

( , ) , , - . , operator new operator delete, , . - , , , shared_ptr , , , operator delete .

: ? , , , , . - , new delete, shared_ptr use_count.

: new delete , . , , , ; , .

, : ( ), . new , - .

, - ( ) , , () , , , .

, " ++" - ( , - ). Boost:: pool, , , .

+4

, 1, unique().

+3

, , Foo , use_count(). , use_count() 1, ? shared_ptr, , . assert(unique()) , .

+2

, shared_ptr , . , use_count.

-, shared_ptr vector, shared_ptr .

Boost shared_ptr.

+2

, , shared_ptr use_count , . , , shared_ptr, , , . , , , , , , .

0

Source: https://habr.com/ru/post/1747542/


All Articles