CSingleLock 类

注释

Microsoft基础类(MFC)库将继续受到支持。 但是,我们不再添加功能或更新文档。

表示多线程程序中用于控制对一个资源的访问的访问控制机制。

语法

class CSingleLock

成员

公共构造函数

名称 描述
CSingleLock::CSingleLock 构造 CSingleLock 对象。

公共方法

名称 描述
CSingleLock::IsLocked 确定对象是否已锁定。
CSingleLock::Lock 等待同步对象。
CSingleLock::Unlock 释放同步对象。

注解

CSingleLock 没有基类。

若要使用同步类 CSemaphore、CMutex、CCriticalSection 和 CEvent,必须创建 CSingleLock 或 CMultiLock 对象来等待并释放同步对象。 如果一次只需等待一个对象,则使用 CSingleLock。 如果有多个对象可在特定时间使用,则使用 CMultiLock。

若要使用 CSingleLock 对象,请在受控资源的类中的构造函数内调用其成员函数。 然后调用 IsLocked 成员函数以确定资源是否可用。 如果可用,请继续执行成员函数的其余部分。 如果资源不可用,请等待指定的时间以便系统释放资源,或返回失败。 不再需要使用资源后,如果要再次使用 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();

返回值

如果对象处于锁定状态,则为非零;否则为 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
指定等待同步对象可用(“有信号”)的时间量。 如果 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();
}

CSingleLock::Unlock

释放由 CSingleLock 拥有的同步对象。

BOOL Unlock();

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

参数

lCount
要释放的访问次数。 必须大于 0。 如果指定的值将导致对象的计数超过其最大值,则不会更改计数,并且函数返回 FALSE。

lPrevCount
指向变量以接收同步对象的上一个计数。 如果 NULL,则不会返回上一个计数。

返回值

如果该函数成功,则为非零;否则为 0。

注解

此函数由 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 类