In the parallel processing process, multiple threads operate on the same object, and the operations are independent of each other. However, they must depend on the state of the object, such as enabled or logged on. In this case, the status needs to be referenced and counted to ensure the operation is effective and concurrent. For ease of description, take some code in the <Remote File System> client connection object class (TRFConnObj) as an example: // object status enum TObjState {osInactive = 0, // osClosing = 1 is not enabled, // close osOpening = 2, // open osOpened = 3}; // enabled // reference count plus 1 bool TRFConnObj :: incRefCount () {// initialize bool result = false; // Lock (); if (FState = osOpened) {FRefCount ++; result = true;} Unlock (); // return result returned;} // The reference count minus 1 void TRFConnObj: DecRefCount () {Lock (); if (F RefCount> 0) FRefCount --; Unlock () ;}// Open long TRFConnObj: Open () {// initialize long result = crStateInvalid; bool boolNext = false; // change status Lock (); if (FState = osInactive) {FState = osOpening; boolNext = true; FRefCount = 0;} else if (FState = osOpened) result = crSuccess; Unlock (); // determine whether to continue if (boolNext) {// execute to open boolNext = false; result = DoOpen (); // change the status Lock (); if (FState! = OsOpening) boolNext = true; else if (result = crSuccess) FState = osOpened; else FState = osInactive; Unlock (); // determine whether to disable if (boolNext) {// execute close if (result = crSuccess) {DoClose (true); result = crFailure;} // change the status (no locks required) FState = osInactive ;}} // return result;} // disable void TRFConnObj: Close (bool ANeedWait) {// initialize bool boolNext = false; bool boolWait = false; // change the status Lock (); if (FState = osOpened) {FState = osClosing; boolNext = true;} else if (FState = osOpening) {FState = osClosing; boolWait = true;} else if (FState = osClosing) boolWait = ANeedWait; Unlock (); // determines whether to wait for if (boolWait) {// wait for Close to end while (FState = osClosing) Sleep (15);} else if (boolNext) {// execute Close DoClose (true ); // change the status (no locks required) FState = osInactive;} // method of Parallel Operation long TRFConnObj :: (...) {// Initialize long result = crStateInvalid; // reference count plus 1 if (IncRefCount () {// operation //??? ...... // The reference count minus 1 DecRefCount ();} // return result;} The Open and Close methods can be understood as the door to opening and closing parallel processing, it is valid only after Open. When you call the Close method, if there are other threads that are operating on the relevant method, it will not be closed until the operation is completed, if you have called the Close method and the status is osClosing and other threads start to operate the method, the system will directly return an invalid status and reject the request. The combination of object status and reference count improves the concurrency of event processing, does not cause exceptions in the boundary status, and ensures the concurrent performance of the system. The reference count and status are often used in the development of server programs. If you fully understand the State Processing in Open and Close, it will be helpful for flexible application to software development with high concurrency. <Remote File System> provides the complete source code, as shown in the following code: Remote File System server Source Code [http://download.csdn.net/detail/kyee/5425478] Remote File System Client source code [TRFConnObj: // return value and error code enum TRFCResult {crSuccess = 1, // success crFailure = 0, // failure crUnknown =-1, // unknown error crNotExist =-2, // does not exist (for example, AConnObj) crNotStart =-3, // the server does not start crNotConnect =-4 ,// CrNonsupport =-5 is not enabled for the connection, // crVersion =-6 is not supported, // The version is too low. crPassword =-7, // The password is incorrect. criexisted =-8, // There is already criillegal =-9, // invalid crAttrInvalid =-10, // The property is invalid crStateInvalid =-11, // The status is invalid crHandleInvalid =-12, // The handle is invalid. crAccessIllegal =-13 }; // invalid access //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ /* TRFConnObj-client connection object class */class TRFConnObj {public: TRFConnObj (TRFConnEvent * AEventObj = NULL, const char * AHost = NULL, long APort = 0, const char * APassword = NULL); virtual ~ TRFConnObj (); // attribute void * Data () const {return FData;} TRCConnection * ConnObj () const {return FConnObj;} TRFConnEvent * EventObj () const {return FEventObj ;} longword CallTimeout () const {return FCallTimeout;} TObjState State () const {return FState;} KYString RFVersion () const {return FRFInfo. version;} TDateTime RFStartTime () const {return FRFInfo. startTime;} TDateTime RFDateTime (); // set the property void SetData (void * AData) {FData = AData;} void SetCallTimeout (Longword ATimeout ); // Open/Close the connection long Open (); void Close (bool ANeedWait = false); // read/set the File Attribute long GetFileAttr (const char * AFileName, long * Attrib ); long SetFileAttr (const char * AFileName, long Attrib); // The file exists/deletes/moves the file or directory long FileExisted (const char * AFileName ); long DeleteFile (const char * AFileName); long MoveFile (const char * AOldName, const char * ANewName); // directory existence/creation/deletion long DirExisted (const char * APathName ); long CreateDir (const char * APathName, bool AForced = false); long RemoveDir (const char * APathName, bool AForced = false ); // functions related to disk operations: long DriveType (const char * ADrive, long * AType); long DiskSpace (const char * APath, _ int64 * ATotalSpace, _ int64 * AFreeSpace ); protected: // status Lock void Lock () const {FLock-> Enter ();} void Unlock () const {FLock-> Leave ();} // increase or decrease the number of objects long IncObjTimes () {return InterlockedIncrement (& FObjTimes);} long DecObjTimes () {return InterlockedDecrement (& FObjTimes );} // add or remove the reference count bool IncRefCount_Valid (); bool IncRefCount (); void DecRefCount (); // read the RF information void GetRFInfo (); void GetRFDateTime (); // Method for accessing the RF attribute long GetRFInt (long Attrib, long & AValue); long GetRFStr (long Attrib, KYString & AValue); long SetRFInt (long Attrib, long AValue ); long SetRFStr (long Attrib, const char * AValue, long ALen); private: // execute initialization/release void DoInit (); void DoFreeing () {FEventObj-> DoFreeing (this);} // execute open/close long DoOpen (); void DoClose (bool ANeedClose); // execute disconnect void DoDisconnect (); void CallOnDisconnect (); // callback Method for executing RC events void DoRecvData (const void * AData, long ASize); private: void * FData; // custom data TKYCritSect * FLock; // status lock TRCConnection * FConnObj; // RC connection object TRFConnEvent * FEventObj; // event object TObjState FState; // status long FObjTimes; // number of objects long FRefCount; // reference count Longword FCallTimeout; // call timeout (millisecond) TRFInfo FRFInfo; // store Server Information Longword FLastTick; // read the tick value of the last RFDateTime TDateTime FDeltaTime; // The current time difference of the storage server TRFCOnDisconnect FOnDisconnect; // OnDisconnect callback event function private: // The callback function of the RC connection static void _ stdcall _ DoRCCallback (long AConnID, long AEvent, long ACmdID, long AReturn); static void _ stdcall _ DoRCRecvData (long AConnID, long ASize, const void * AData );};