CSingleLock::Lock

呼叫此函式來保存物件控制之資源的存取權給 CSingleLock 建構函式。

BOOL Lock(
   DWORD dwTimeOut = INFINITE 
);

參數

  • dwTimeOut
    指定時間等待同步物件可用 (表示)。如果 INFINITE, Lock 等待,直到物件在傳回之前先收到信號。

傳回值

如果不是零,則函式成功,則為 0。

備註

如果同步物件發出信號, Lock 會成功傳回,且執行緒目前擁有物件。如果未收到信號的同步處理物件 (無法使用), Lock 等待同步物件收到信號 dwTimeOut 由參數所指定的毫秒數量為止。如果同步物件在指定的時間內沒有收到信號, Lock 傳回失敗。

範例

// 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();
}

需求

Header: afxmt.h

請參閱

參考

CSingleLock 類別

階層架構圖