Rhino C++ API  7.21
Public Member Functions | List of all members
ON_PointerSleepLock Class Reference

#include <opennurbs_plus_sleeplock.h>

Public Member Functions

 ON_PointerSleepLock ()=default
 ~ON_PointerSleepLock ()=default
ON__UINT_PTR GetPointerOrLock (ON__UINT_PTR *address_of_shared_resource_ptr, unsigned int interval_wait_msecs, unsigned int max_wait_msecs, bool bStealLockAfterWaiting)
bool SetPointerAndUnlock (ON__UINT_PTR valid_shared_resource_ptr)

Constructor & Destructor Documentation

◆ ON_PointerSleepLock()

ON_PointerSleepLock::ON_PointerSleepLock ( )

You should put your ON_PointerSleepLock on the stack.
Using managed memory to construct / destruct these locks is a good way to lock something important and loose the key.

◆ ~ON_PointerSleepLock()

ON_PointerSleepLock::~ON_PointerSleepLock ( )

Member Function Documentation

◆ GetPointerOrLock()

ON__UINT_PTR ON_PointerSleepLock::GetPointerOrLock ( ON__UINT_PTR *  address_of_shared_resource_ptr,
unsigned int  interval_wait_msecs,
unsigned int  max_wait_msecs,
bool  bStealLockAfterWaiting 

Description: Gets the lock using a the specified waiting interval and maximum waiting time. Parameters: address_of_shared_resource_ptr - [in] The address of a shared resource pointer cast as (ON__UINT_PTR*).

interval_wait_msecs - [in] number of milliseconds to wait between checking for the lock. If interval_wait_msecs is 0, then 50 is used. If interval_wait_msecs > max_wait_msecs > 0, then one attempt is made to get the lock.

max_wait_msecs - [in] maximum number of milliseconds to wait for lock. If max_wait_msecs is 0, then no maximum wating time is used.

bStealLockAfterWaiting - [in] Steal the lock if the resource is not available after the maximum waiting time expires.

Returns: 0: The shared resource is locked by another process and the maximum waiting time expired.

1: The shared resource pointer was nullptr and is now locked or the maximum waiting time expired and bStealLockAfterWaiting was true. You need to create the resource as quickly as possible and call SetPointerAndUnlock() with a pointer to the new resource.

If GetLock() returns 1, then you must call SetPointerAndUnlock(). Remarks: It is easy and clear to people reading your code if you use values like ON_SleepLock::OneSecond, ON_SleepLock::ThirtySeconds, ON_SleepLock::OneMinute, 47*ON_SleepLock::OneSecond or ON_SleepLock::OneMinute/3 to specify the values for interval_wait_msecs and max_wait_msecs.


const ON_3dPoint* SharedResourcePointAt123() { static ON_3dPoint* shared_resource = nullptr;

ON_PointerSleepLock lock;

ON__UINT_PTR ptr = lock.GetLock( (ON__UINT_PTR*)&shared_resource, ... );

if ( 1 == ptr ) { / Create the shared resource ON_3dPoint* valid_point = new ON_3dPoint(1,2,3); lock.ReturnLock( (ON__UINT_PTR)valid_point ); }

return shared_resource; }

◆ SetPointerAndUnlock()

bool ON_PointerSleepLock::SetPointerAndUnlock ( ON__UINT_PTR  valid_shared_resource_ptr)

Description: Unconditionally returns the lock. Returns: True if a lock was released.