次の方法で共有


CCriticalSection::Lock

クリティカル セクション オブジェクトのアクセス権を取得します。

BOOL Lock( ); 
BOOL Lock(
   DWORD dwTimeout 
);

パラメーター

  • dwTimeout
    Lock では、このパラメーターの値は無効です。

戻り値

正常終了した場合は 0 以外を返します。それ以外の場合は 0 を返します。

解説

Lock は、クリティカル セクション オブジェクトがシグナル状態になる (有効になる) まで戻らないブロッキング呼び出しです。

時間待ちが必要な場合、CCriticalSection オブジェクトの代わりに CMutex オブジェクトを使用します。

Lock が必要なシステム メモリの割り当てに失敗すると、CMemoryException 型のメモリ例外が自動的にスローされます。

使用例

共有 CCriticalSection オブジェクトを使って共有リソース (静的 _strShared オブジェクト) へのアクセスを制御して、クリティカル セクションを入れ子にするアプローチの例を次に示します。 SomeMethod 関数は、共有リソースを安全な方法で更新します。

//Definition of critical section class
class CMyCritSectClass
{
   static CString _strShared; //shared resource
   static CCriticalSection _critSect;
public:
   CMyCritSectClass(void) {}
   ~CMyCritSectClass(void) {}
   void SomeMethod(void); //locks, modifies, and unlocks shared resource
};

//Declaration of static members and SomeMethod
CString CMyCritSectClass::_strShared;
CCriticalSection CMyCritSectClass::_critSect;

void CMyCritSectClass::SomeMethod()
{
   _critSect.Lock();
   if (_strShared == "")
      _strShared = "<text>";
   _critSect.Unlock();
}

必要条件

**ヘッダー:**afxmt.h

参照

参照

CCriticalSection クラス

階層図

CSingleLock::Lock

その他の技術情報

CCriticalSection のメンバー