共用方式為


CSingleLock::Lock

Call this function to gain access to the resource controlled by the synchronization object supplied to the CSingleLock constructor.

BOOL Lock( 
   DWORD dwTimeOut = INFINITE  
);

Parameters

  • dwTimeOut
    Specifies the amount of time to wait for the synchronization object to be available (signaled). If INFINITE, Lock will wait until the object is signaled before returning.

Return Value

Nonzero if the function was successful; otherwise 0.

Remarks

If the synchronization object is signaled, Lock will return successfully and the thread now owns the object. If the synchronization object is nonsignaled (unavailable), Lock will wait for the synchronization object to become signaled up to the number of milliseconds specified in the dwTimeOut parameter. If the synchronization object did not become signaled in the specified amount of time, Lock returns failure.

Example

// m_Mutex is a data member (of type CMutex)
// of an existing class that implements the resource being shared.

// Relate the synchronization object (m_Mutex) with
// our CSingleLock object. 
CSingleLock singleLock(&m_Mutex);

// Attempt to lock the shared resource
if (singleLock.Lock(100))    // Wait 100 ms...
{
    // We were able to lock the resource;
    // we may now work with the data associated with the mutex...

    // Now that we are finished, unlock the resource for others.
    singleLock.Unlock();
}

Requirements

Header: afxmt.h

See Also

Reference

CSingleLock Class

Hierarchy Chart