Smart Pointers

scope_lock Class Reference

#include <sync_wrapper.hpp>

List of all members.


Detailed Description

The scope_lock can be used with lock policy to lock a pointer within a scope.
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, mutex_wrapper
Note:
When scope_lock goes out of scope, it's destructor is called, and it unlocks the smart_ptr.

Definition at line 130 of file sync_wrapper.hpp.

Public Member Functions

template<class T>
 scope_lock (T &smrtPtr, bool LockIt=true)
 ~scope_lock ()


The documentation for this class 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]