CMutex クラスCMutex Class
"Mutex" を表します。これは、1つのスレッドがリソースに相互に排他的にアクセスできるようにする同期オブジェクトです。Represents a "mutex" — a synchronization object that allows one thread mutually exclusive access to a resource.
構文Syntax
class CMutex : public CSyncObject
メンバーMembers
パブリック コンストラクターPublic Constructors
名前Name | 説明Description |
---|---|
CMutex:: CMutexCMutex::CMutex | CMutex オブジェクトを構築します。Constructs a CMutex object. |
解説Remarks
ミューテックスは、一度に1つのスレッドのみがデータまたはその他の制御されたリソースを変更できるようにする場合に便利です。Mutexes are useful when only one thread at a time can be allowed to modify data or some other controlled resource. たとえば、リンクリストへのノードの追加は、一度に1つのスレッドによってのみ許可されるプロセスです。For example, adding nodes to a linked list is a process that should only be allowed by one thread at a time. オブジェクトを使用して CMutex
リンクリストを制御することにより、一度に1つのスレッドだけがリストにアクセスできます。By using a CMutex
object to control the linked list, only one thread at a time can gain access to the list.
オブジェクトを使用するには CMutex
、 CMutex
必要に応じてオブジェクトを構築します。To use a CMutex
object, construct the CMutex
object when it is needed. 待機するミューテックスの名前を指定し、アプリケーションで最初に所有する必要があります。Specify the name of the mutex you wish to wait on, and that your application should initially own it. その後、コンストラクターから制御が戻ったときに mutex にアクセスできます。You can then access the mutex when the constructor returns. 制御されたリソースへのアクセスが完了したら、 CSyncObject:: Unlock を呼び出します。Call CSyncObject::Unlock when you are done accessing the controlled resource.
オブジェクトを使用する別の方法として、 CMutex
CMutex
制御するクラスにデータメンバーとして型の変数を追加する方法があります。An alternative method for using CMutex
objects is to add a variable of type CMutex
as a data member to the class you wish to control. 制御されたオブジェクトの構築時に、 CMutex
ミューテックスが最初に所有されているかどうか、ミューテックスの名前 (プロセスの境界を越えて使用される場合)、および必要なセキュリティ属性を指定して、データメンバーのコンストラクターを呼び出します。During construction of the controlled object, call the constructor of the CMutex
data member specifying if the mutex is initially owned, the name of the mutex (if it will be used across process boundaries), and desired security attributes.
この方法でオブジェクトによって制御されるリソースにアクセスするに CMutex
は、まず、 CSingleLock 型の変数を作成するか、リソースのアクセスメンバー関数に CMultiLock を入力します。To access resources controlled by CMutex
objects in this manner, first create a variable of either type CSingleLock or type CMultiLock in your resource's access member function. 次に、lock オブジェクトの Lock
メンバー関数 (たとえば、 CSingleLock:: lock) を呼び出します。Then call the lock object's Lock
member function (for example, CSingleLock::Lock). この時点で、スレッドはリソースへのアクセスを取得し、リソースが解放されてアクセスできるまで待機します。または、リソースが解放され、タイムアウトになるまで待機し、リソースへのアクセスを取得できなくなります。At this point, your thread will either gain access to the resource, wait for the resource to be released and gain access, or wait for the resource to be released and time out, failing to gain access to the resource. いずれの場合も、スレッドセーフな方法でリソースにアクセスしています。In any case, your resource has been accessed in a thread-safe manner. リソースを解放するには、lock オブジェクトの Unlock
メンバー関数 (たとえば、 CSingleLock:: Unlock) を使用するか、ロックオブジェクトがスコープ外に出ることを許可します。To release the resource, use the lock object's Unlock
member function (for example, CSingleLock::Unlock), or allow the lock object to fall out of scope.
オブジェクトの使用方法の詳細については、 CMutex
「 マルチスレッド: 同期クラスの使用方法」を参照してください。For more information on using CMutex
objects, see the article Multithreading: How to Use the Synchronization Classes.
継承階層Inheritance Hierarchy
CMutex
要件Requirements
ヘッダー: afxmtHeader: afxmt.h
CMutex:: CMutexCMutex::CMutex
名前付きオブジェクトまたは名前のないオブジェクトを構築 CMutex
します。Constructs a named or unnamed CMutex
object.
CMutex(
BOOL bInitiallyOwn = FALSE,
LPCTSTR lpszName = NULL,
LPSECURITY_ATTRIBUTES lpsaAttribute = NULL);
パラメーターParameters
ビン分割bInitiallyOwn
オブジェクトを作成するスレッド CMutex
が、最初にミューテックスによって制御されるリソースにアクセスできるかどうかを指定します。Specifies if the thread creating the CMutex
object initially has access to the resource controlled by the mutex.
lpszNamelpszName
CMutex
オブジェクトの名前。Name of the CMutex
object. 同じ名前の別のミューテックスが存在する場合、オブジェクトがプロセスの境界を越えて使用される場合は、 Lpszname を指定する必要があります。If another mutex with the same name exists, lpszName must be supplied if the object will be used across process boundaries. NULL の場合、ミューテックスは無名になります。If NULL, the mutex will be unnamed. 名前が既存のミューテックスと一致する場合、コンストラクターは CMutex
その名前のミューテックスを参照する新しいオブジェクトを作成します。If the name matches an existing mutex, the constructor builds a new CMutex
object which references the mutex of that name. 名前がミューテックスではない既存の同期オブジェクトと一致する場合、構築は失敗します。If the name matches an existing synchronization object that is not a mutex, the construction will fail.
lpsaAttributelpsaAttribute
Mutex オブジェクトのセキュリティ属性。Security attributes for the mutex object. この構造の詳細については、Windows SDK の「 SECURITY_ATTRIBUTES 」を参照してください。For a full description of this structure, see SECURITY_ATTRIBUTES in the Windows SDK.
解説Remarks
オブジェクトにアクセスしたり解放したりするには CMutex
、 CMultiLock オブジェクトまたは CSingleLock オブジェクトを作成し、その Lock および Unlock メンバー関数を呼び出します。To access or release a CMutex
object, create a CMultiLock or CSingleLock object and call its Lock and Unlock member functions. CMutex
オブジェクトがスタンドアロンで使用されている場合は、その Unlock
メンバー関数を呼び出して解放します。If the CMutex
object is being used stand-alone, call its Unlock
member function to release it.
重要
オブジェクトを作成した後 CMutex
、 GetLastError を使用して、ミューテックスが既に存在していないことを確認します。After creating the CMutex
object, use GetLastError to ensure that the mutex did not already exist. ミューテックスが予期せずに存在する場合は、不正なプロセスがスクワッティングであることを示している可能性があります。また、ミューテックスを悪用する可能性があります。If the mutex did exist unexpectedly, it may indicate a rogue process is squatting and may be intending to use the mutex maliciously. この場合、推奨されるセキュリティ意識の高い手順は、ハンドルを閉じて、オブジェクトの作成でエラーが発生したかのように続行することです。In this case, the recommended security-conscious procedure is to close the handle and continue as if there was a failure in creating the object.