/* COTD - Proxy Object Code - by D. Sim Dietrich Jr. [simmer@pacbell.net] Here is an example to handle creating and freeing locked down memory in Win32 : */ class GlobalAllocProxy() { private HGLOBAL hMem; private bool bLocked; public GlobalAllocProxy( const int& size ) { hMem = GlobalAlloc( size ); }; public void* Lock() { void* pMem = 0; if ( !mbLocked && ( hMem != 0 ) ) { pMem = GlobalLock( hMem ); mbLocked = ( pMem != 0 ); } return pMem; } public void Unlock() { if ( mbLocked ) { GlobalUnlock( hMem ); mbLocked = false; } } public ~GlobalAllocProxy() { if ( hMem ) { UnLock(); GlobalFree( hMem ); } } }; // Class GlobalAllocProxy Here is an example that shows how to use an abstract base class to support portability for CriticalSections. You could use a typedef or #define to make the type created completely hidden from the main code. // The following code piece was contributed by Bobris: class Mutex { public: Mutex() { InitializeCriticalSection(&mCS); } ~Mutex() { DeleteCriticalSection(&mCS); } void enter() { EnterCriticalSection(&mCS); } void leave() { LeaveCriticalSection(&mCS); } private: CRITICAL_SECTION mCS; }; class Lock { public: Lock(Mutex &aMutex): mMutex(aMutex) { aMutex.enter(); } ~Lock() { mMutex.leave(); } private: Mutex &mMutex; };