Home > Back-end >  Interlock quantity through the constructor plus unlock and class object directly and unlock have why
Interlock quantity through the constructor plus unlock and class object directly and unlock have why

Time:11-09

Encounter a mutual lock and unlock the amount of code, there is confusion, strives for the answer, according to the following code, through the constructor for the incoming class object plus unlock operation, but direct to add the class PdfMutex object isn't unlock operation can also? Why must be through the constructor to add the unlock? And the difference, thank you for your answer!


The following code in the constructor PdfMutexWrapper: : reference to the privatization is class PdfMutexWrapper variable PdfMutexWrapper PdfMutex& M_rMutex initialization list type, and then in the constructor calls the reference variable m_rMutex member function Lock, compile times wrong, cannot use an incomplete type; But to compile also pass it after complete types, an error "cannot be defined in its class external static members", solve, thank you all for the great god!

The class PdfMutexWrapper
{
Public:

/* * Lock a mutex.
*
* \ param rMutex the mutex to be locked.
*/
PODOFO_NOTHROW inline PdfMutexWrapper (PdfMutex & amp; RMutex);

/* * Unlocks the mutex on destruction
*/
The inline ~ PdfMutexWrapper ();

Private:

PdfMutex& M_rMutex;
};

PdfMutexWrapper: : PdfMutexWrapper (PdfMutex & amp; RMutex) : m_rMutex (rMutex)
{
M_rMutex. The Lock ();
}

CodePudding user response:

The first question: "through the constructor for the incoming class object plus unlock operation, but direct to add the class PdfMutex object isn't unlock operation can also? Why must be through the constructor to add the unlock? And the difference, "search RAII, is what you want to answer, for the lock, don't have to write their own MutexWrapper since c + + 11, the standard library provides STD: : lock_guard, STD: : unique_lock template can be used, such as
The second question: the error message "cannot be defined in its class external static members" has clearly pointed out the mistake, the code has a non-static data members outside the class definition, search, will soon be able to solve,

CodePudding user response:

Thanks big!
  • Related