📄 classpsafeobject.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>PSafeObject class Reference</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.2.18 --><center><a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center><hr><h1>PSafeObject Class Reference</h1><code>#include <<a class="el" href="safecoll_8h-source.html">safecoll.h</a>></code><p><p>Inheritance diagram for PSafeObject:<p><center><img src="classPSafeObject.png" usemap="#PSafeObject_map" border="0" alt=""></center><map name="PSafeObject_map"><area href="classPObject.html" alt="PObject" shape="rect" coords="0,0,83,24"></map><a href="classPSafeObject-members.html">List of all members.</a><table border=0 cellpadding=0 cellspacing=0><tr><td></td></tr><tr><td colspan=2><br><h2>Public Methods</h2></td></tr><tr><td colspan="2"><div class="groupHeader">Construction</div></td></tr><tr><td nowrap align=right valign=top> </td><td valign=bottom><a class="el" href="classPSafeObject.html#z319_0">PSafeObject</a> ()</td></tr><tr><td colspan="2"><div class="groupHeader">Operations</div></td></tr><tr><td nowrap align=right valign=top>BOOL </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_0">SafeReference</a> ()</td></tr><tr><td nowrap align=right valign=top>void </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_1">SafeDereference</a> ()</td></tr><tr><td nowrap align=right valign=top>BOOL </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_2">LockReadOnly</a> () const</td></tr><tr><td nowrap align=right valign=top>void </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_3">UnlockReadOnly</a> () const</td></tr><tr><td nowrap align=right valign=top>BOOL </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_4">LockReadWrite</a> ()</td></tr><tr><td nowrap align=right valign=top>void </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_5">UnlockReadWrite</a> ()</td></tr><tr><td nowrap align=right valign=top>void </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_6">SafeRemove</a> ()</td></tr><tr><td nowrap align=right valign=top>BOOL </td><td valign=bottom><a class="el" href="classPSafeObject.html#z321_7">SafelyCanBeDeleted</a> () const</td></tr><tr><td colspan=2><br><h2>Protected Attributes</h2></td></tr><tr><td nowrap align=right valign=top><a class="el" href="classPMutex.html">PMutex</a> </td><td valign=bottom><a class="el" href="classPSafeObject.html#n0">safetyMutex</a></td></tr><tr><td nowrap align=right valign=top>unsigned </td><td valign=bottom><a class="el" href="classPSafeObject.html#n1">safeReferenceCount</a></td></tr><tr><td nowrap align=right valign=top>BOOL </td><td valign=bottom><a class="el" href="classPSafeObject.html#n2">safelyBeingRemoved</a></td></tr><tr><td nowrap align=right valign=top><a class="el" href="classPReadWriteMutex.html">PReadWriteMutex</a> </td><td valign=bottom><a class="el" href="classPSafeObject.html#n3">safeInUseFlag</a></td></tr></table><hr><a name="_details"></a><h2>Detailed Description</h2>This class defines a thread-safe object in a collection.<p>This is part of a set of classes to solve the general problem of a collection (eg a PList or PDictionary) of objects that needs to be a made thread safe. Any thread can add, read, write or remove an object with both the object and the database of objects itself kept thread safe.<p>The act of adding a new object is simple and only requires locking the collection itself during the add.<p>Locating an object is more complicated. The obvious lock on the collection is made for the initial search. But we wish to have the full collection lock for as short a period as possible (for performance reasons) so we lock the individual object and release the lock on the collection.<p>A simple mutex on the object however is very dangerous as it can be (and should be able to be!) locked from other threads independently of the collection. If one of these threads subsequently needs to get at the collection (eg it wants to remove the object) then we will have a deadlock. Also, to avoid a race condition with the object begin deleted, the objects lock must be made while the collection lock is set. The performance gains are then lost as if something has the object locked for a long time, then another object wanting that object will actually lock the collection for a long time as well.<p>So, an object has 4 states: unused, referenced, reading & writing. With the additional rider of "being removed". This flag prevents new locks from being acquired and waits for all locks to be relinquished before removing the object from the system. This prevents numerous race conditions and accesses to deleted objects.<p>The "unused" state indicates the object exists in the collection but no threads anywhere is using it. It may be moved to any state by any thread while in this state. An object cannot be deleted (ie memory deallocated) until it is unused.<p>The "referenced" state indicates that a thread has a reference (eg pointer) to the object and it should not be deleted. It may be locked for reading or writing at any time thereafter.<p>The "reading" state is a form of lock that indicates that a thread is reading from the object but not writing. Multiple threads can obtain a read lock. Note the read lock has an implicit "reference" state in it.<p>The "writing" state is a form of lock where the data in the object may be changed. It can only be obtained exclusively and if there are no read locks present. Again there is an implicit reference state in this lock.<p>Note that threads going to the "referenced" state may do so regardless of the read or write locks present.<p>Access to safe objects (especially when in a safe collection) is recommended to by the <a class="el" href="classPSafePtr.html">PSafePtr</a><> class which will manage reference counting and the automatic unlocking of objects ones the pointer goes out of scope. It may also be used to lock each object of a collection in turn.<p>The enumeration <p><hr><h2>Constructor & Destructor Documentation</h2><a name="z319_0" doxytag="PSafeObject::PSafeObject"></a><p><table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> PSafeObject::PSafeObject </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top">) </td> <td class="md" nowrap></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Create a thread safe object. </td> </tr></table><hr><h2>Member Function Documentation</h2><a name="z321_2" doxytag="PSafeObject::LockReadOnly"></a><p><table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> BOOL PSafeObject::LockReadOnly </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top">) </td> <td class="md" nowrap></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Lock the object for Read Only access. This will lock the object in read only mode. Multiple threads may lock the object read only, but only one thread can lock for read/write. Also, no read only threads can be present for the read/write lock to occur and no read/write lock can be present for any read only locks to occur.<p>If the function returns FALSE, then the object has been flagged for deletion and the calling thread should immediately cease use of the object, possibly executing the <a class="el" href="classPSafeObject.html#z321_1">SafeDereference()</a> function to remove any references it may have acquired.<p>It is expected that the caller had already called the <a class="el" href="classPSafeObject.html#z321_0">SafeReference()</a> function (directly or implicitly) before calling this function. It is recommended that the <a class="el" href="classPSafePtr.html">PSafePtr</a><> class is used to automatically manage the reference counting and locking of objects. </td> </tr></table><a name="z321_4" doxytag="PSafeObject::LockReadWrite"></a><p><table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> BOOL PSafeObject::LockReadWrite </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top">) </td> <td class="md" nowrap></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Lock the object for Read/Write access. This will lock the object in read/write mode. Multiple threads may lock the object read only, but only one thread can lock for read/write. Also no read only threads can be present for the read/write lock to occur and no read/write lock can be present for any read only locks to occur.<p>If the function returns FALSE, then the object has been flagged for deletion and the calling thread should immediately cease use of the object, possibly executing the <a class="el" href="classPSafeObject.html#z321_1">SafeDereference()</a> function to remove any references it may have acquired.<p>It is expected that the caller had already called the <a class="el" href="classPSafeObject.html#z321_0">SafeReference()</a> function (directly or implicitly) before calling this function. It is recommended that the <a class="el" href="classPSafePtr.html">PSafePtr</a><> class is used to automatically manage the reference counting and locking of objects. </td> </tr></table><a name="z321_1" doxytag="PSafeObject::SafeDereference"></a><p><table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> void PSafeObject::SafeDereference </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top">) </td> <td class="md" nowrap></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Decrement the reference count for object. This indicates that the thread no longer has anything to do with the object and it may be deleted (ie memory deallocated).<p>It is recommended that the <a class="el" href="classPSafePtr.html">PSafePtr</a><> class is used to manage this rather than the application calling this function directly. </td> </tr></table><a name="z321_7" doxytag="PSafeObject::SafelyCanBeDeleted"></a><p><table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> BOOL PSafeObject::SafelyCanBeDeleted </td> <td class="md" valign="top">( </td> <td class="mdname1" valign="top" nowrap> </td> <td class="md" valign="top">) </td> <td class="md" nowrap></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Determine if the object can be safely deleted. This determines if the object has been flagged for deletion and all references to it have been released.<p>This is typically used by the <a class="el" href="classPSafeCollection.html">PSafeCollection</a> class and is not expected to be used directly by an application. </td> </tr></table><a name="z321_0" doxytag="PSafeObject::SafeReference"></a><p><table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -