세마포어-초기 카운트의 사용은 무엇입니까?
http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim.aspx
세마포어를 만들려면 초기 개수와 최대 개수를 제공해야합니다. MSDN은 초기 횟수는 다음과 같습니다.
동시에 부여 할 수있는 세마포에 대한 초기 요청 수입니다.
최대 개수는
동시에 부여 할 수있는 세마포에 대한 최대 요청 수입니다.
최대 개수는 리소스에 동시에 액세스 할 수있는 스레드의 최대 개수라는 것을 알 수 있습니다. 그러나 초기 카운트의 사용은 무엇입니까?
초기 개수가 0이고 최대 개수가 2 인 세마포를 만들면 스레드 풀 스레드가 리소스에 액세스 할 수 없습니다. 초기 개수를 1로 설정하고 최대 개수를 2로 설정하면 스레드 풀 스레드 만 리소스에 액세스 할 수 있습니다. 초기 개수와 최대 개수를 모두 2 개로 설정 한 경우에만 2 개의 스레드가 동시에 리소스에 액세스 할 수 있습니다. 그래서, 나는 초기 카운트의 중요성에 대해 정말로 혼란 스럽습니까?
SemaphoreSlim semaphoreSlim = new SemaphoreSlim(0, 2); //all threadpool threads wait
SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 2);//only one thread has access to the resource at a time
SemaphoreSlim semaphoreSlim = new SemaphoreSlim(2, 2);//two threadpool threads can access the resource concurrently
예, 초기 숫자가 0으로 설정되면 "CurrentCount"속성을 증가시키는 동안 모든 스레드가 대기합니다. Release () 또는 Release (Int32)를 사용하여 수행 할 수 있습니다.
Release (...)-세마포어 카운터를 증가시킵니다.
Wait (...)-감소합니다
초기화에서 설정 한 최대 개수보다 큰 카운터 ( "CurrentCount"속성)를 증가시킬 수 없습니다.
예를 들면 :
SemaphoreSlim^ s = gcnew SemaphoreSlim(0,2); //s->CurrentCount = 0
s->Release(2); //s->CurrentCount = 2
...
s->Wait(); //Ok. s->CurrentCount = 1
...
s->Wait(); //Ok. s->CurrentCount = 0
...
s->Wait(); //Will be blocked until any of the threads calls Release()
그래서, 나는 초기 카운트의 중요성에 대해 정말로 혼란 스럽습니까?
여기서 도움이 될 수있는 한 가지 중요한 점 Wait은 세마포어 수를 Release줄이고 증가시키는 것입니다.
initialCount즉시 허용되는 리소스 액세스 수입니다. 즉, Wait세마포어가 인스턴스화 된 직후 블로킹없이 호출 할 수있는 횟수입니다 .
maximumCount세마포어가 얻을 수있는 최대 개수입니다. 이것은 배의 개수 Release가정 던지고 예외없이 호출 될 수있는 initialCount수는 0이었다. 경우 initialCount와 같은 값으로 설정 maximumCount한 후 호출 Release세마포어는 예외가 발생합니다 인스턴스화 직후.
한 번에 리소스에 액세스 할 수있는 스레드 수를 원하십니까? 초기 카운트를 그 숫자로 설정하십시오. 이 숫자가 프로그램 수명 동안 절대 증가하지 않을 경우 최대 개수도 해당 숫자로 설정하십시오. 이렇게하면 리소스를 해제하는 방법에 프로그래밍 오류가있는 경우 프로그램이 충돌하여 알려줍니다.
(두 개의 생성자가 있습니다. 하나는 초기 값 만 취하는 것이고 다른 하나는 추가적으로 최대 개수를 취하는 것입니다. 어느 것이 든 적절한 것을 사용하십시오.)
이런 식으로 현재 스레드가 세마포어를 만들 때 시작부터 일부 리소스를 요청할 수 있습니다.
스레드가 일정 시간 동안 리소스에 액세스하지 않도록하려면 초기 카운트를 0으로 전달하고 세마포를 생성 한 직후에 모든 스레드에 액세스 권한을 부여하려면 최대 카운트와 동일한 초기 카운트 값을 전달합니다. . 예를 들면 :
hSemaphore = CreateSemaphoreA(NULL, 0, MAX_COUNT, NULL) ;
//Do something here
//No threads can access your resource
ReleaseSemaphore(hSemaphore, MAX_COUNT, 0) ;
//All threads can access the resource now
MSDN 설명서에 인용 된대로- "ReleaseSemaphore의 또 다른 사용은 응용 프로그램의 초기화 중에 있습니다. 응용 프로그램은 초기 개수가 0 인 세마포어를 만들 수 있습니다. 이렇게하면 세마포어의 상태가 신호 없음으로 설정되고 모든 스레드가 보호 된 리소스에 액세스하는 것을 차단합니다. 초기화를 마치면 ReleaseSemaphore를 사용하여 개수를 최대 값으로 늘리고 보호 된 리소스에 대한 정상적인 액세스를 허용합니다. "
세마포어는 리소스 풀을 보호하는 데 사용할 수 있습니다 . 리소스 풀을 사용 하여 데이터베이스 연결과 같이 생성 비용 이 많이 드는 것을 재사용 합니다.
So initial count refers to the number of available resources in the pool at the start of some process. When you read the initialCount in code you should be thinking in terms of how much up front effort are you putting into creating this pool of resources.
I am really confused about the significance of initial count?
Initial count = Upfront cost
As such, depending on the usage profile of your application, this value can have a dramatic effect on the performance of your application. It's not just some arbitrary number.
You should think carefully about what you creating, how expensive they are to create and how many you need right away. You should literally able able to graph the optimal value for this parameter and should likely think about making it configurable so you can adapt the performance of the process to the time at which it is being executed.
As MSDN explains it under the Remarks section:
If initialCount is less than maximumCount, the effect is the same as if the current thread had called WaitOne (maximumCount minus initialCount) times. If you do not want to reserve any entries for the thread that creates the semaphore, use the same number for maximumCount and initialCount.
So If the initial count is 0 and max is 2 it is as if WaitOne has been called twice by the main thread so we have reached capacity (semaphore count is 0 now) and no thread can enter Semaphore. Similarly If initial count is 1 and max is 2 WaitOnce has been called once and only one thread can enter before we reach capacity again and so on.
If 0 is used for initial count we can always call Release(2) to increase the semaphore count to max to allow maximum number of threads to acquire resource.
참고URL : https://stackoverflow.com/questions/4706734/semaphore-what-is-the-use-of-initial-count
'Nice programing' 카테고리의 다른 글
| ASP.NET 애플리케이션을 단위 테스트 할 때 web.config를 사용하는 방법 (0) | 2020.11.01 |
|---|---|
| 왜 (0) | 2020.11.01 |
| 실행 가능한 jar 파일을 만드는 방법은 무엇입니까? (0) | 2020.11.01 |
| 데이터 클래스는 무엇이며 공통 클래스와 어떻게 다릅니 까? (0) | 2020.11.01 |
| .net : System.Web.Mail 대 System.Net.Mail (0) | 2020.11.01 |