Semaphore - What is the use of an opening account?

http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim.aspx

To create a semaphore, I need to specify an initial counter and a maximum quantity. MSDN claims the start counter is

The initial number of queries for a semaphore that can be provided at the same time.

While it is indicated that the maximum quantity

The maximum number of queries for a semaphore that can be provided simultaneously.

I can understand that the maximum number is the maximum number of threads that can access the resource at the same time. But what is the use of an initial account?

If I create a semaphore with an initial number of 0 and a maximum of 2, none of my stream threads will be able to access the resource. If I set the initial count as 1, and the maximum number is 2, then the resource stream of the thread can access the resource. Only when I set both the initial count and the maximum count as 2, 2 threads can simultaneously access the resource. So, am I really confused about the value of the original account?

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 
+49
multithreading c # concurrency semaphore
Jan 16 '11 at 17:03
source share
6 answers

Yes, when the seed is set to 0, all threads will wait until you increase the CurrentCount property. You can do this with Release () or Release (Int32).

Release (...) - increase the semaphore counter

Wait (...) - will reduce it

You cannot increment the counter (the "CurrentCount" property) beyond the maximum amount that you set during initialization.

For example:

 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() 
+43
Jan 16 '11 at 17:51
source share

So, am I really confused about the value of the opening account?

An important point that can help here is that Wait reduces the number of semaphores and Release increases it.

initialCount is the amount of access to resources that will be resolved immediately. Or, in other words, this number of times Wait can be called without locking immediately after creating the semaphore.

maximumCount is the highest score a semaphore can get. This number of times Release can be called without throwing an exception if the initialCount count is zero. If the initialCount parameter initialCount set to the same value as maximumCount , then calling Release immediately after creating the semaphore will throw an exception.

+27
Feb 20 2018-12-12T00:
source share

How many threads do you want to access the resource at once? Set your opening account to this number. If this number will never increase throughout the life of the program, set the maximum number of masks to this number. Thus, if you have a programming error in how you free the resource, your program will fail and inform you.

(There are two constructors: one that accepts only the initial value, and one that additionally accepts the maximum value. Use what works).

+4
Jan 16 '11 at 20:14
source share

Thus, when the current thread creates a semaphore, it may require some resources from the very beginning.

+1
Jan 16 2018-11-17T00:
source share

If you want no thread to access your resource for some time, you pass the initial count as 0 and when you want to provide access to all of them immediately after creating the semaphore, you pass the value of the initial count equal to the maximum number. For example:

 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 

As stated in the MSDN documentation: β€œAnother use of ReleaseSemaphore during application initialization. An application can create a semaphore with an initial number of zero. This sets the semaphore to an unaligned state and blocks all threads from accessing a protected resource. When the application finishes its initialization, it uses ReleaseSemaphore to increase counter to its maximum value to ensure normal access to the protected resource. "

+1
Feb 20 2018-12-12T00:
source share

As MSDN explains this in the Notes section:

If the initialCount value is less than the maximum, then the effect will be the same as if the current thread called WaitOne (maximum Count minus initialCount) times. If you do not want to reserve entries for the stream that creates the semaphore, use the same number for maximumCount and initialCount.

So, if the initial count is 0, and max is 2, then, as if WaitOne was called twice on the main thread, we reached the capacity (the number of semaphores is now 0), and not a single thread can enter the Semaphore. Similarly, if the initial count is 1 and max is 2, WaitOnce was called once and only one thread can enter before we reach capacity again and so on.

If 0 is used for the initial count, we can always call Release (2) to increase the semaphore count to max so that the maximum number of threads can receive the resource.

0
Mar 10 '17 at 1:22
source share



All Articles