c++-gtk-utils
|
This is a generic class for managing the lifetime of objects allocated on freestore, with a thread safe reference count.. More...
#include <c++-gtk-utils/shared_handle.h>
Public Member Functions | |
SharedLockHandle (T ptr=0) | |
SharedLockHandle (T ptr, Cgu::SharedHandleAllocFail::Leave tag) | |
void | reset (T ptr=0) |
void | reset (T ptr, Cgu::SharedHandleAllocFail::Leave tag) |
SharedLockHandle (const SharedLockHandle &sh_hand) | |
SharedLockHandle (SharedLockHandle &&sh_hand) | |
SharedLockHandle & | operator= (SharedLockHandle sh_hand) |
T | get () const |
operator T () const | |
unsigned int | get_refcount () const |
~SharedLockHandle () |
This is a generic class for managing the lifetime of objects allocated on freestore, with a thread safe reference count..
Class SharedLockHandle is a version of the SharedHandle class which includes locking so that it can be accessed in multiple threads (although the word Lock is in the title, by default it uses glib atomic functions to access the reference count rather than a mutex, so the overhead should be very small). Note that only the reference count is protected, so this is thread safe in the sense in which a raw pointer is thread safe. A shared handle accessed in one thread referencing a particular object is thread safe as against another shared handle accessing the same object in a different thread. It is thus suitable for use in different Std C++ containers which exist in different threads but which contain shared objects by reference. But:
1. If the referenced object is to be modified in one thread and read or modified in another thread an appropriate mutex for the referenced object is required (unless that referenced object does its own locking).
2. If the same instance of shared handle is to be modified in one thread (by assigning to the handle so that it references a different object, or by moving from it), and copied (assigned from or used as the argument of a copy constructor), accessed, destroyed or modified in another thread, a mutex for that instance of shared handle is required.
3. Objects referenced by shared handles which are objects for which POSIX provides no guarantees (in the main, those which are not built-in types), such as strings and similar containers, may not support concurrent reads in different threads. That depends on the library implementation concerned. If that is the case, a mutex for the referenced object will also be required when reading any given instance of such an object in more than one thread by dereferencing any shared handles referencing it (and indeed, when not using shared handles at all).
SharedLockHandle objects can be instantiated for pointers to constant objects (such as SharedLockHandle<const char*>), provided the deleter functor will take such pointers.
This library provides StandardArrayDelete, CFree, GFree, GerrorFree, GSliceFree, GSliceFreeSize and GSliceDestroy deleter functors, which can be used as the second template parameter of the SharedLockHandle class. StandardArrayDelete is the default.
As mentioned, by default glib atomic functions are used to provide thread-safe manipulation of the reference count. However, a library user can define the symbol CGU_SHARED_LOCK_HANDLE_USE_MUTEX before shared_handle.h is parsed so as to use mutexes instead, which might be useful for some debugging purposes.
Comparison with std::shared_ptr
Although the semantics of std::shared_ptr in C++11 are not particularly suited to managing either arrays or C objects with accessor functions (such as in glib), most of the things that can be done by this class can be done by using std::shared_ptr with a specialised deleter. However, this class is retained in the c++-gtk-utils library not only to retain compatibility with series 1.2 of the library, but also to cater for some cases not met (or not so easily met) by std::shared_ptr:
(i) The Cgu::SharedLockHandle class takes its deleter as a template parameter, which means that typedefs can be used to enable handles for particular deleters to be easily created (and as mentioned, this library provides a number of pre-formed deleter functors and typedefs for them). With std::shared_ptr, custom deleters must be passed to the shared_ptr constructor on every occasion a shared_ptr is constructed to manage a new object (and they cannot be templated as a typedef).
(ii) Glib memory slices provide an efficient small object allocator (they are likely to be significantly more efficient than global operator new()/new[](), which generally hand off to malloc(), and whilst malloc() is good for large block allocations it is generally poor as a small object allocator). Internal Cgu::SharedLockHandle allocation using glib memory slices can be achieved by compiling the library with the --with-glib-memory-slices-no-compat configuration option.
(iii) If glib memory slices are not used (which do not throw), constructing a shared pointer for a new managed object (or calling reset() for a new managed object) might throw if internal allocation fails. Although by default the Cgu::SharedLockHandle implementation will delete the new managed object in such a case, it also provides an alternative constructor and reset() method which instead enable the new object to be accessed via the thrown exception object so that user code can decide what to do; std::shared_ptr deletes the new object in every case.
(iv) A user can explicitly state whether the shared handle object is to have atomic increment and decrement-and-test with respect to the reference count so that the reference count is thread safe ('no' in the case of Cgu::SharedHandle, and 'yes' in the case of Cgu::SharedLockHandle). Using atomic functions is unnecessary if the managed object concerned is only addressed in one thread (and might cause unwanted cache flushing in certain circumstances). std::shared_ptr will generally always use atomic functions with respect to its reference count in a multi-threaded program.
In favour of std::shared_ptr, it has an associated std::weak_ptr class, which Cgu::SharedLockHandle does not (there is a Cgu::GobjWeakHandle class, but that is cognate with Cgu::GobjHandle and is only usable with GObjects). In addition shared_ptr objects have some atomic store, load and exchange functions provided for them which enable concurrent modifications of the same instance of shared_ptr in different threads to have defined results.
Cgu::SharedLockHandle< T, Dealloc >::SharedLockHandle | ( | T | ptr = 0 | ) | [inline, explicit] |
Constructor taking an unmanaged object.
ptr | The object which the SharedLockHandle is to manage (if any). |
std::bad_alloc | This constructor will not throw if the 'ptr' argument has a NULL value (the default), otherwise it might throw std::bad_alloc if memory is exhausted and the system throws in that case. If such an exception is thrown, this constructor is exception safe (it does not leak resources), but as well as cleaning itself up this constructor will also delete the managed object passed to it to avoid a memory leak. If such automatic deletion is not wanted in that case, use the version of this constructor taking a Cgu::SharedHandleAllocFail::Leave tag argument. |
Cgu::SharedLockHandle< T, Dealloc >::SharedLockHandle | ( | T | ptr, |
Cgu::SharedHandleAllocFail::Leave | tag | ||
) | [inline] |
Constructor taking an unmanaged object.
ptr | The object which the SharedLockHandle is to manage. |
tag | Passing the tag emumerator Cgu::SharedHandleAllocFail::leave causes this constructor not to delete the new managed object passed as the 'ptr' argument in the event of internal allocation in this method failing because of memory exhaustion (in that event, Cgu::SharedHandleError will be thrown). |
Cgu::SharedHandleError | This constructor might throw Cgu::SharedHandleError if memory is exhausted and the system would otherwise throw std::bad_alloc in that case. This constructor is exception safe (it does not leak resources), and if such an exception is thrown it will clean itself up, but it will not attempt to delete the new managed object passed to it. Access to the object passed to the 'ptr' argument can be obtained via the thrown Cgu::SharedHandleError object. |
Cgu::SharedLockHandle< T, Dealloc >::SharedLockHandle | ( | const SharedLockHandle< T, Dealloc > & | sh_hand | ) | [inline] |
The copy constructor does not throw.
sh_hand | The handle to be copied. |
Cgu::SharedLockHandle< T, Dealloc >::SharedLockHandle | ( | SharedLockHandle< T, Dealloc > && | sh_hand | ) | [inline] |
The move constructor does not throw. It has move semantics.
sh_hand | The handle to be moved. |
Cgu::SharedLockHandle< T, Dealloc >::~SharedLockHandle | ( | ) | [inline] |
The destructor does not throw unless the destructor of a handled object throws - that should never happen.
T Cgu::SharedLockHandle< T, Dealloc >::get | ( | ) | const [inline] |
This method does not throw.
unsigned int Cgu::SharedLockHandle< T, Dealloc >::get_refcount | ( | ) | const [inline] |
This method does not throw.
Cgu::SharedLockHandle< T, Dealloc >::operator T | ( | ) | const [inline] |
This method does not throw.
SharedLockHandle& Cgu::SharedLockHandle< T, Dealloc >::operator= | ( | SharedLockHandle< T, Dealloc > | sh_hand | ) | [inline] |
This method (and so copy or move assignment) does not throw unless the destructor of a managed object throws.
sh_hand | the assignor. |
void Cgu::SharedLockHandle< T, Dealloc >::reset | ( | T | ptr = 0 | ) | [inline] |
Causes the SharedLockHandle to cease to manage its managed object (if any), deleting it if this is the last ShareLockHandle object managing it. If the argument passed is not NULL, the SharedLockHandle object will manage the new object passed (which must not be managed by any other SharedLockHandle object).
ptr | NULL (the default), or a new unmanaged object to manage. |
std::bad_alloc | This method will not throw if the 'ptr' argument has a NULL value (the default) and the destructor of a managed object does not throw, otherwise it might throw std::bad_alloc if memory is exhausted and the system throws in that case. Note that if such an exception is thrown then this method will do nothing (it is strongly exception safe and will continue to manage the object it was managing prior to the call), except that it will delete the new managed object passed to it to avoid a memory leak. If such automatic deletion in the event of such an exception is not wanted, use the reset() method taking a Cgu::SharedHandleAllocFail::Leave tag type as its second argument. |
void Cgu::SharedLockHandle< T, Dealloc >::reset | ( | T | ptr, |
Cgu::SharedHandleAllocFail::Leave | tag | ||
) | [inline] |
Causes the SharedLockHandle to cease to manage its managed object (if any), deleting it if this is the last ShareLockHandle object managing it. The SharedLockHandle object will manage the new object passed (which must not be managed by any other SharedLockHandle object). This method is exception safe, but see the comments below on Cgu::SharedHandleError.
ptr | A new unmanaged object to manage (if no new object is to be managed, use the version of reset() taking a default value of NULL). |
tag | Passing the tag emumerator Cgu::SharedHandleAllocFail::leave causes this method not to delete the new managed object passed as the 'ptr' argument in the event of internal allocation in this method failing because of memory exhaustion (in that event, Cgu::SharedHandleError will be thrown). |
Cgu::SharedHandleError | This method might throw Cgu::SharedHandleError if memory is exhausted and the system would otherwise throw std::bad_alloc in that case. Note that if such an exception is thrown then this method will do nothing (it is strongly exception safe and will continue to manage the object it was managing prior to the call), and it will not attempt to delete the new managed object passed to it (if any). Access to the object passed to the 'ptr' argument can be obtained via the thrown Cgu::SharedHandleError object. |