共用方式為


CSemaphore::CSemaphore

Constructs a named or unnamed CSemaphore object.

CSemaphore(
   LONG lInitialCount = 1,
   LONG lMaxCount = 1,
   LPCTSTR pstrName = NULL,
   LPSECURITY_ATTRIBUTES lpsaAttributes = NULL 
);

Parameters

  • lInitialCount
    The initial usage count for the semaphore. Must be greater than or equal to 0, and less than or equal to lMaxCount.

  • lMaxCount
    The maximum usage count for the semaphore. Must be greater than 0.

  • pstrName
    The name of the semaphore. Must be supplied if the semaphore will be accessed across process boundaries. If NULL, the object will be unnamed. If the name matches an existing semaphore, the constructor builds a new CSemaphore object which references the semaphore of that name. If the name matches an existing synchronization object that is not a semaphore, the construction will fail.

  • lpsaAttributes
    Security attributes for the semaphore object. For a full description of this structure, see SECURITY_ATTRIBUTES in the Windows SDK.

Remarks

To access or release a CSemaphore object, create a CMultiLock or CSingleLock object and call its Lock and Unlock member functions.

Security noteSecurity Note:

After creating the CSemaphore 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.

Requirements

Header: afxmt.h

See Also

Concepts

CSemaphore Class

CSemaphore Members

Hierarchy Chart

CMutex Class

CEvent Class

CMultiLock Class

CSingleLock Class