Multithreading Smart Pointer
The smart_ptr is one of the few smart pointers that have both reference counting/linking logic and synchronization logic. This allows developers to use a reference logic smart pointer in a multithreading environment.
The lock logic locks both the pointee and the smart pointer itself at the same time. Although smart_ptr mainly uses intrusive lock logic, it still can be used with any type by using the mutex_wrapper class.
The mutex_wrapper class gives intrusive lock logic to any type.
In the above code, the vector<int> type is given intrusive lock logic through the mutex_wrapper class. The shared_ptr_policy class is declared with the intrusive_lock_policy class.
smart_ptr<mutex_wrapper<vector<int> >, shared_ptr_policy<ref_link_policy, intrusive_lock_policy> > SharedSmartPointer = new mutex_wrapper<vector<int> >;
To lock the smart_ptr, the scope_lock class can be used.
When the above code leaves the if-loop, the MyLock object will go out of scope, and release the lock for SharedSmartPointer. Both the mutex_wrapper class and the scope_lock class are declared in the sync_wrapper.hpp header. Win32 developers can also use the critical_sect_wrapper class, which is also listed in the sync_wrapper.hpp header.
scope_lock MyLock = SharedSmartPointer;
//code using SharedSmartPointer in multithread code
Generated on Wed Mar 29 21:58:59 2006 for Smart Pointers by