tailieunhanh - HandBooks Professional Java-C-Scrip-SQL part 42

Tham khảo tài liệu 'handbooks professional java-c-scrip-sql part 42', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | public some_class std cout some_class some_class n some_class const some_class other std cout some_class const some_class other n some_class std cout some_class some_class n int main std cout Before start of scope n boost intrusive_ptr some_class p1 new some_class boost intrusive_ptr some_class p2 p1 std cout After end of scope n To demonstrate that the intrusive_ptrs together with the functions intrusive_ptr_add_ref and intrusive_ptr_release do their jobs right here is the output from running the program Before start of scope some_class some_class some_class some_class After end of scope The intrusive_ptr is taking care of business for us. When the first intrusive_ptr pl is created it is passed a new instance of some_class. The intrusive_ptr constructor actually takes two arguments. The second is a bool that states whether intrusive_ptr_add_ref should be called or not. Because the default value of this argument is TRue when constructing pl the reference counter for the instance of some_class becomes 1. Then a second intrusive_ptr p2 is constructed. It is copy constructed from pl and when p2 sees that pl is referencing a non-null pointer it calls intrusive_ptr_add_ref. The reference count is now 2. Then the two intrusive_ptrs leave scope. First p2 is destroyed and the destructor calls intrusive_ptr_release. This decrements the reference counter to 1. Then pl is destroyed and the destructor calls intrusive_ptr_release again which causes the reference count to drop to 0 this in turn triggers our implementation of intrusive_ptr_release to delete the pointer. You ll note that the implementation of reference_counter is not thread-safe and therefore cannot be used in multithreaded applications without adding synchronization. Rather than relying on a generic implementation of intrusive_ptr_add_ref and intrusive_ptr_release we could have these functions operate directly on the base class here reference_counter . The advantage of this approach is that even if the classes .

TÀI LIỆU LIÊN QUAN
10    158    1
6    184    1
7    162    1
5    157    1
6    160    1
6    152    1
6    150    1
6    206    1
7    154    1