This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14970|Reply: 3

[Source] [Actual combat]. NET/C# uses SemaphoreSlim to limit concurrent execution methods

[Copy link]
Posted on 2/19/2022 9:23:20 PM | | | |
Requirements: Due to the limited resources of the hardware, the program needs to consume a certain amount of hardware resources, in the case of high concurrency, if some methods that consume too much resources of the program are not restricted (exporting a large amount of data into an Excel table), it may affect the inability of the whole program to provide normal services, and also affect the normal operation of the operating system.

Semaphore and SemaphoreSlim Difference

SemaphoreSlim and Semaphore are similar in functionality. SemaphoreSlim is about 4 times faster than Semaphore, but SemaphoreSlim cannot be used for inter-process signaling.

The reason for the performance improvement is because the SemaphoreSlim class provides a lightweight alternative to the Semaphore class, which does not use the Windows kernel semaphore. Essentially, if you don't need to name semaphores, use the SemaphoreSlim class.

SemaphoreSlim is based on SpinWait and Monitor, so a thread waiting to get a lock consumes a period of CPU cycles, hoping to acquire the lock before giving way to another thread. If this doesn't happen, then the thread lets the system switch the context and try again when the OS schedules that thread again (by consuming some CPU cycles). If the wait is long, this mode can consume a lot of CPU cycles. So the best case for this implementation is that most of the time there is no waiting time and you can get the lock almost immediately.
Semaphore relies on implementations in the OS kernel, so each time a lock is acquired, it takes quite a bit of CPU cycles, but after that the thread simply hibernates to get the time it takes to get the lock.

Semaphore Documentation:https://docs.microsoft.com/en-us ... aphore?view=net-6.0
SemaphoreSlim Documentation:https://docs.microsoft.com/en-us ... reslim?view=net-6.0

Before executing the method, the commonly used waiting methods are as followsCall according to the actual situation of your own business

Wait(): Blocks the thread until it can enter SemaphoreSlim.
Wait(0): The method will not be blocked. It will test the state of the waiting handle and return immediately.
Wait: Blocks the current thread until it can enter SemaphoreSlim, while specifying a timeout using a 32-bit signed integer.

The source code is as follows:

The renderings are as follows:







Previous:Linux Secretless Login (ssh) or Secretless Copy (scp) files to other servers
Next:Linux uses lookbusy to simulate CPU usage
Posted on 2/19/2022 11:16:57 PM |
Learn to learn...
Posted on 2/20/2022 10:19:24 PM |
test
 Landlord| Posted on 12/18/2024 8:40:10 PM |
The difference between ManualResetEvent and AutoResetEvent learned by C# threads
https://www.itsvse.com/thread-4810-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com