C # multithreading-semaphores (Semaphore ),
Baidu Encyclopedia: SemaphoreIt is responsible for coordinating various threads to ensure they can use public resources correctly and reasonably. It is also used in the operating system to control the amount of synchronization mutex between processes.
SemaphoreThere are two common methodsWaitOne ()AndRelease (),The function of Release () is to exit the semaphore and return the previous count, while WaitOne () is to stop the current thread until the WaitHandle of the current thread receives the signal. Here I will give you an example to make it easier to understand: When we instantiate Semaphore like this
Semaphore sema = new Semaphore(x,y)
When a queue of people goes to the bathroom, they are equivalent to threads. x indicates the number of remaining positions, and y indicates the total number of positions.
The WaitOne () method is equivalent to waiting for a person to wait for the restroom location, while the Release () method is equivalent to a person coming out of the restroom. Here we assume that both x and y are 5, it indicates that there are five empty locations in the restroom at the beginning, and there are only five positions in total. When a team of more than five members need to go to the restroom, they will queue up. FirstWaitOne () methodWait. If there is any vacant space, go in order, and subtract one from each vacant space, until there is no vacant space after 5. At this time, the people who enter will wait until they come out of the bathroom.Release () method,Vacant Space plus one, waitingWaitOne () methodThe user finds another vacant space and deletes one ...... This is cyclical.
See the following example:
Public class Program {static Semaphore sema = new Semaphore (5, 5); const int cycleNum = 9; static void Main (string [] args) {for (int I = 0; I <cycleNum; I ++) {Thread td = new Thread (new ParameterizedThreadStart (testFun); td. name = string. format ("No. {0}", I. toString (); td. start (td. name);} Console. readKey ();} public static void testFun (object obj) {sema. waitOne (); Console. writeLine (obj. toString () + "Enter the bathroom:" + DateTime. now. toString (); Thread. sleep (1, 2000); Console. writeLine (obj. toString () + "restroom:" + DateTime. now. toString (); sema. release ();}}The running result is as follows:
Public int Release (int releaseCount );
ReleaseCount refers to the number of released semaphores. If no parameter is set to 1 by default, Release () is equivalent to Release (1)
Here, we need to note that when Release () or Release (int releaseCount) is executed, the SemaphoreFullException exception will be thrown when the semaphore count is greater than the maximum number. The following exception occurs:
Semaphore sem = new Semaphore (); sem. Release (2); // here is the Release of 2 semaphores plus the previous 4, more than 5
public virtual bool WaitOne(TimeSpan timeout);public virtual bool WaitOne(int millisecondsTimeout);
The first overload parameter timeout: specifies the time interval. If no signal is received during this time period, skip and wait for execution to continue.
The second overload parameter millisecondsTimeout: the specified time interval is an integer in milliseconds. If no signal is received during this time period, skip and wait for execution to continue.
WaitOne () has two overload methods which are not very common and will not be introduced here. The above overload method is no longer mentioned here. If you are interested, try it yourself.
Author: Yifeng Jianbai
Source: C # multithreading-semaphores (Semaphore)
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable. If you have any questions or suggestions, please give me some further information. Thank you very much.