Smart Pointers

intrusive_lock_policy Struct Reference

#include <smart_ptr.hpp>

List of all members.


Detailed Description

This implements an intrusive lock policy. Allows synchronization of the smart pointer and the pointee.
The intrusive_lock_policy requires the user to implement two intrusive lock functions that take the pointer type.
void intrusive_ptr_lock(T * p);
void intrusive_ptr_unlock(T * p);
Where T is the target type, and the functions are in a namespace accessible to the lock policy.
Optionally, the following functions can also be implemented, but are not required:
bool intrusive_ptr_trylock(T * p); bool intrusive_ptr_islock(T * p);
Example Usage.

#include "../smartptr/smart_ptr.hpp"
#include "../smartptr/sync_wrapper.hpp"

smart_ptr<mutex_wrapper<vector<int> >, copy_on_write_policy<ref_link_policy, intrusive_lock_policy> > SharedSmartPointer = new mutex_wrapper<vector<int> >;
int ThreadIDIndex = 10;

void example_intrusive_lock_policy_usage(void*) //example_intrusive_lock_policy_usage is a thread executed via _beginthread
{
        const size_t MaxSizeAllowed = 2000000;
        int ThreadId = ThreadIDIndex++;
        int SleepTime = rand()% 30;
        int QtyToAdd = rand()% 1000;
        Sleep(SleepTime);
        for(;;)
        {
                int ValidateResults = 0;
                scope_lock MyLocked = SharedSmartPointer;
                size_t StartPos = SharedSmartPointer->size();
                for(int i = 0;i < QtyToAdd;++i)
                {
                        int AddValue = rand()% 9999;
                        ValidateResults += AddValue;
                        SharedSmartPointer->push_back(AddValue);
                }
                int ValidateResults_2nd = 0;
                for(int i = 0;i < QtyToAdd;++i)
                {
                        ValidateResults_2nd += (*SharedSmartPointer)[StartPos + i];
                }
                //              cout << "ID("<< ThreadId << "): size = " << (int)SharedSmartPointer->size() << 
                //                      " V1 = " << ValidateResults <<  " V2 = " << ValidateResults_2nd <<      endl;
                if (ValidateResults != ValidateResults_2nd)
                {
                        cout << "ID("<< ThreadId << "): Error :" << 
                                " V1 = " << ValidateResults <<  " V2 = " << ValidateResults_2nd <<      endl;
                        system("pause");
                        exit(-1);
                }
                if (SharedSmartPointer->size() > MaxSizeAllowed) 
                {
                        cout << "ThreadId(" << ThreadId << "): Clearing Vect, size = " << (int)SharedSmartPointer->size() << endl;
                        SharedSmartPointer->clear();
                }
        }
}

void Test_intrusive_lock_policy()
{
        srand((unsigned) time(NULL));
        const int QtyTestThreads = 20;
        cout << "Starting Synchronize Smart Pointer Test" << endl;
        for(int i = 0;i < QtyTestThreads;++i)
                _beginthread(example_intrusive_lock_policy_usage, 0, NULL); 

        for(;;) Sleep(9000);
}

See also:
smart_ptr, no_lock_policy
Note:
The intrusive lock policy will work with ALL ownership policy types.

Definition at line 704 of file smart_ptr.hpp.

Public Member Functions

 intrusive_lock_policy ()
template<class T_obj>
void pointee_lock_policy (T_obj *p)
template<class T_obj>
void const_pointee_lock_policy (T_obj *p)
template<class T_obj>
void assignment_lock_policy (T_obj *p)
template<class T_obj>
void assignment_unlock_policy (T_obj *p)
template<class T_obj>
void destructor_lock_policy (T_obj *p)
template<class T_obj>
void destructor_unlock_policy (T_obj *p)
template<class T_obj>
T_obj * constructor_lock_policy (T_obj *p)
template<class T_obj>
void constructor_unlock_policy (T_obj *p)
template<class T_obj>
void lock (T_obj *p)
template<class T_obj>
void unlock (T_obj *p)
template<class T_obj>
bool trylock (T_obj *p)
template<class T_obj>
bool islock (T_obj *p)


The documentation for this struct was generated from the following file:
Generated on Wed Mar 29 21:58:59 2006 for Smart Pointers by  doxygen 1.4.6.Axter [Axter-Extended-Version]