logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

CreateSemaphore Semaphore Manager

by digipine posted Oct 29, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

 

The CreateSemaphore function creates a named or unnamed semaphore object.

HANDLE CreateSemaphore(
  LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
                       // pointer to security attributes
  LONG lInitialCount,  // initial count
  LONG lMaximumCount,  // maximum count
  LPCTSTR lpName       // pointer to semaphore-object name
);
 
Parameters
lpSemaphoreAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSemaphoreAttributes is NULL, the handle cannot be inherited.

Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new semaphore. If lpSemaphoreAttributes is NULL, the semaphore gets a default security descriptor.

lInitialCount
Specifies an initial count for the semaphore object. This value must be greater than or equal to zero and less than or equal to lMaximumCount. The state of a semaphore is signaled when its count is greater than zero and nonsignaled when it is zero. The count is decreased by one whenever a wait function releases a thread that was waiting for the semaphore. The count is increased by a specified amount by calling the ReleaseSemaphore function.
lMaximumCount
Specifies the maximum count for the semaphore object. This value must be greater than zero.
lpName
Pointer to a null-terminated string specifying the name of the semaphore object. The name is limited to MAX_PATH characters, and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.

If lpName matches the name of an existing named semaphore object, this function requests SEMAPHORE_ALL_ACCESS access to the existing object. In this case, the lInitialCount and lMaximumCountparameters are ignored because they have already been set by the creating process. If the lpSemaphoreAttributesparameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.

If lpName is NULL, the semaphore object is created without a name.

If lpName matches the name of an existing event, mutex, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

Return Values

 

If the function succeeds, the return value is a handle to the semaphore object. If the named semaphore object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

 

The handle returned by CreateSemaphore has SEMAPHORE_ALL_ACCESS access to the new semaphore object and can be used in any function that requires a handle to a semaphore object.

Any thread of the calling process can specify the semaphore-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.

The state of a semaphore object is signaled when its count is greater than zero, and nonsignaled when its count is equal to zero. The lInitialCount parameter specifies the initial count. Each time a waiting thread is released because of the semaphore's signaled state, the count of the semaphore is decreased by one. Use the ReleaseSemaphore function to increment a semaphore's count by a specified amount. The count can never be less than zero or greater than the value specified in the lMaximumCount parameter.

Multiple processes can have handles of the same semaphore object, enabling use of the object for interprocess synchronization. The following object-sharing mechanisms are available:

  • A child process created by the CreateProcess function can inherit a handle to a semaphore object if the lpSemaphoreAttributes parameter of CreateSemaphore enabled inheritance.
  • A process can specify the semaphore-object handle in a call to the DuplicateHandle function to create a duplicate handle that can be used by another process.
  • A process can specify the name of a semaphore object in a call to the OpenSemaphore or CreateSemaphore function.

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The semaphore object is destroyed when its last handle has been closed. 

 
TAG •

List of Articles
No. Subject Author Date Views
45 Customizing GINA, Part 2 digipine 2017.10.28 96712
44 C# 으로 구현한 화면 캡춰 클래스 1 digipine 2017.11.02 33970
43 [C#] 코드 실행 시간 측정 및 DateTime 스트링으로 변환 포맷 lizard2019 2019.01.23 23018
42 Customizing GINA, Part 1 digipine 2017.10.28 21759
41 VC++ UTF8 변환 관련 매크로 digipine 2017.11.02 8779
40 The .Net Developer's Guide to Directory Services Programming digipine 2017.10.29 7171
39 [C#] 프로그램 종료 방법 lizard2019 2019.01.23 6789
38 [API Hooking] Dll Injection 하는 방법 digipine 2017.10.29 5226
37 [WIN32] Process ID로 HWND 구하기 digipine 2017.10.29 5019
36 [WIN32] 실행 중인 프로세스를 외부에서 강제로 종료, 안전한 TerminateProcess digipine 2017.10.29 3460
35 [Windows] DOS 명령어 실행하고 결과 스트링 가져오는 샘플 코드 digipine 2017.11.02 2689
34 VC++(MFC)에서 MDB 생성 / 압축 / 연동관리자 digipine 2017.10.29 2654
33 [WIN32] API Hook 정리 문서 digipine 2017.10.29 1970
32 [VC++, WInAPI] 폴더를 통채로 지우기, 서브 폴더 포함, DeleteAllFiles digipine 2017.10.29 1763
31 [WIN32, WINCE] 디스크 용량 구하는 방법 API GetDiskFreeSpaceEx digipine 2017.10.29 1569
30 [Win API]프로세스 아이디와 윈도우 핸들을 이용 파일명 구하기 digipine 2017.10.29 1393
29 C# - 한글로된 폰트명 처리 방법 개선 (Font Name Localization) digipine 2017.11.02 1392
28 RPC에 대하여... (1) : RPC 가 사용하는 TCP/IP 포트는 ? digipine 2017.10.29 1295
27 GINA(Graphical Identification aNd Authentication), SAS(Secure Attention Sequence) digipine 2017.10.29 1258
26 RPC에 대하여... (2) : RPC 가 사용하는 포트를 바꿔보자 digipine 2017.10.29 1118
Board Pagination Prev 1 2 3 Next
/ 3