다음을 통해 공유


CSingleLock 클래스

다중 스레드 프로그램에서 한 리소스에 대한 액세스를 제어할 때 사용하는 액세스 제어 메커니즘을 나타냅니다.

구문

class CSingleLock

멤버

공용 생성자

속성 설명
CSingleLock::CSingleLock CSingleLock 개체를 생성합니다.

공용 메서드

이름 설명
CSingleLock::IsLocked 개체가 잠겨 있는지 여부를 확인합니다.
CSingleLock::Lock 동기화 개체를 기다립니다.
CSingleLock::Unlock 동기화 개체를 해제합니다.

설명

CSingleLock 에는 기본 클래스가 없습니다.

동기화 클래스CSemaphoreCMutexCEventCCriticalSection사용하려면 동기화 개체를 대기하고 해제할 개체 CSingleLockCMultiLock 만들어야 합니다. 한 번에 하나의 개체에서만 대기해야 하는 경우에 사용합니다 CSingleLock . 특정 시간에 사용할 수 있는 여러 개체가 있는 경우 사용합니다 CMultiLock .

개체를 CSingleLock 사용하려면 제어된 리소스 클래스의 멤버 함수 내에서 해당 생성자를 호출합니다. 그런 다음 멤버 함수를 IsLocked 호출하여 리소스를 사용할 수 있는지 확인합니다. 이 경우 멤버 함수의 re기본der를 계속합니다. 리소스를 사용할 수 없는 경우 리소스가 해제될 때까지 지정된 시간 동안 기다리거나 오류를 반환합니다. 리소스 사용이 완료되면 개체를 Unlock 다시 사용할 경우 함수를 CSingleLock 호출하거나 개체를 CSingleLock 제거하도록 허용합니다.

CSingleLock 개체에는 .에서 CSyncObject파생된 개체가 있어야 합니다. 일반적으로 제어된 리소스 클래스의 데이터 멤버입니다. 개체를 사용하는 CSingleLock 방법에 대한 자세한 내용은 다중 스레딩: 동기화 클래스를 사용하는 방법 문서를 참조하세요.

상속 계층 구조

CSingleLock

요구 사항

헤더afxmt.h:

CSingleLock::CSingleLock

CSingleLock 개체를 생성합니다.

explicit CSingleLock(
    CSyncObject* pObject,
    BOOL bInitialLock = FALSE);

매개 변수

pObject
액세스할 동기화 개체를 가리킵니다. 할 수 없습니다 NULL.

bInitialLock
제공된 개체에 처음 액세스할지 여부를 지정합니다.

설명

이 함수는 일반적으로 제어된 리소스의 액세스 멤버 함수 내에서 호출됩니다.

예시

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

// Relate the synchronization object (m_CritSection) with
// our CSingleLock object. 
CSingleLock singleLock(&m_CritSection);
singleLock.Lock();  // Attempt to lock the shared resource
if (singleLock.IsLocked())  // Resource has been locked
{
   //...use the shared resource...

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

CSingleLock::IsLocked

개체와 CSingleLock 연결된 개체가 서명되지 않은(사용할 수 없음) 여부를 확인합니다.

BOOL IsLocked();

Return Value

개체가 잠겨 있으면 0이 아닌 경우 그렇지 않으면 0입니다.

예시

// 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
singleLock.Lock(100);    // Wait 100 ms...

// Has the resource been successfully locked?
if (singleLock.IsLocked())
{
   // 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();
}

CSingleLock::Lock

생성자에 제공된 동기화 개체에 의해 제어되는 리소스에 액세스하려면 이 함수를 CSingleLock 호출합니다.

BOOL Lock(DWORD dwTimeOut = INFINITE);

매개 변수

dwTimeOut
동기화 개체를 사용할 수 있을 때까지 대기하는 시간을 지정합니다(신호). Lock 이면 INFINITE반환하기 전에 개체가 신호를 받을 때까지 기다립니다.

Return Value

함수가 성공한 경우 0이 아닌 경우 그렇지 않으면 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();
}

CSingleLock::Unlock

소유하는 동기화 개체를 CSingleLock해제합니다.

BOOL Unlock();

BOOL Unlock(
    LONG lCount,
    LPLONG lPrevCount = NULL);

매개 변수

lCount
릴리스할 액세스 수입니다. 0보다 커야 합니다. 지정된 양으로 인해 개체 수가 최대값을 초과하면 개수가 변경되지 않고 함수가 반환됩니다 FALSE.

lPrevCount
동기화 개체의 이전 개수를 받을 변수를 가리킵니다. 이 경우 NULL이전 개수가 반환되지 않습니다.

Return Value

함수가 성공한 경우 0이 아닌 경우 그렇지 않으면 0입니다.

설명

이 함수는 's 소멸자가 호출 CSingleLock합니다.

세마포의 액세스 횟수를 두 개 이상 해제해야 하는 경우 두 번째 형식을 Unlock 사용하고 릴리스할 액세스 수를 지정합니다.

예시

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

참고 항목

계층 구조 차트
CMultiLock 클래스