Smart Pointers

mutex_wrapper Class Reference

#include <sync_wrapper.hpp>


Detailed Description

mutex_wrapper is a class that implements intrusive lock functions for target type. In order to use the intrusive_lock_policy, the target type requires two intrusive lock functions.
void intrusive_ptr_lock(T * p);
void intrusive_ptr_unlock(T * p);
Moreover, the target type also needs to store lock handle(s) and/or lock state. The mutex_wrapper class can be used to automatically add the required lock implementation to the target type, and therefore allow for the use of a synchronized smart pointer on any type.
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, intrusive_lock_policy, scope_lock
Note:
Although the mutex_wrapper is used with intrusive_lock_policy, it can be used with ownership policy types that have ref_count_policy or ref_link_policy as the sub policy.


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]