The semaphore class can control the number of threads that a resource is allowed to access, semaphore named, or unnamed, and if you do not consider cross-process work, you typically use an unnamed method in your code.
The semaphore is a bit like a wait handle, and if a thread calls the WaitOne method, the thread pauses and waits for a semaphore to be available before it resumes execution; a thread calls the release method, releasing a signal count value, releasing one at a time for each call. If you want to release n signals at once, you can call the release (int) overload to pass the quantity to be freed to the method parameter, but the value cannot exceed the maximum value specified by the semaphore instantiation, or an exception will be thrown.
The semaphore constructor can specify the maximum allowable semaphore, as well as the default semaphore amount. The statement reads as follows:
Semaphore (intint maximumcount);
The Maximumcount parameter specifies the maximum amount of semaphores allowed for the object, and the Initialcount parameter specifies a default value that cannot exceed the maximum value specified by Maximumcount. That is, how many threads are allowed to receive a signal (access resource) By default for the semaphore instance.
When a resource-intensive thread calls the release method, it releases one or more signals, and the other waiting threads can continue to execute.
As long as it is related to threading problems are particularly difficult to understand, rather abstract, quite test the ability of human understanding.
For example, there are five "x bottles of Plum" in the library, but there are 20 people who want to borrow the book. The first five people are naturally very easy to borrow (into the access circle, the threads outside the five threads wait), others have to wait.
After a few days, there was a guy reading all night, and finally finished, so he also the book, at this time, the remaining 15 people to see who's action fast, you can borrow the book just returned.
After a few days, another two people have finished reading the book. At this time, two of the remaining 14 people could borrow the book.
The approximate principle is this, see the example below.
classProgram {//generate random numbers to delay the execution time of each task StaticRandom Rand =NewRandom (); //declaring a semaphore variable to control the amount of thread semaphores StaticSemaphore SM =NULL; Static voidMain (string[] args) {SM=NewSemaphore (1,4);//instantiation of//Start 10 Tasks for(inti =0; I <Ten; i++) {Task T=NewTask (DoWork,"Task"+ (i +1)); T.start (); } //prevent DOS windows from exiting immediatelyConsole.read (); } Private Static Async voidDoWork (Objectp) {Sm. WaitOne (); //waiting for flowers to open stringTN = p?. ToString (); Console.WriteLine ($"{TN} has been accessed. "); awaitTask.delay (Rand. Next (1,Ten) * +); //ReleaseSm. Release ();//Thanks, flowers.Console.WriteLine ($"{TN} has been disposed. "); } }
Multi-threaded development My favorite task class, convenient simple and powerful to use on the tall, and it can also handle the CPU multi-core problem. In the example above, there are 10 tasks to execute, but the maximum number of access threads that I have instantiated for the semaphore object is 4, while the default state allows only 1 threads to access simultaneously.
So, when 10 tasks start, one of them will grab access, and the rest will wait. This time the semaphore object has an accessible number of 0. Because only 1 is allowed by default, there is now a thread to rob, so the remaining 0 access rights.
When this access to the task called the release method, Access is released, this time the remaining 9 tasks will start to Rob, who Rob who will execute ... And so on
Look at the results of the operation.
Task 1 deft on hand, it first grabbed access, so it dododo,do, call release method released, and then task 3 good character, grabbed access to the right, and then call release released after Xxxxx,x. Other threads continue to rob ...
After reading the above example, we should have a clue.
Now, let's change the code above to change the default value from 1 to 3 when initializing the semaphore object.
New Semaphore (34);
By default, 3 threads are allowed to access resources at the same time, with a maximum number of 4.
Then run again with the following results:
Because the default allows 3 threads to enter simultaneously, in the output, the first three tasks can gain access, while other tasks can only wait for the opportunity. After one or N of the three tasks in which the current face has been given a resource, the remaining tasks begin to seize the opportunity.
This article is an example: Http://files.cnblogs.com/files/tcjiaan/DemoApp.zip
【. NET deep Breathing "thread semaphore (Semaphore)