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

View: 193519|Reply: 11

[Source] .net/c# User multithreaded concurrency lock(string){...} Detailed explanation

[Copy link]
Posted on 7/3/2019 3:25:19 PM | | | |
Common misuse scenarios: In order to prevent duplicate payment of orders caused by users accidentally clicking the payment button multiple times, we use lock (order number) to ensure that only one thread is allowed to perform the operation on the order.

This idea is good, at least better than lock (a private static object for processing classes), because the effect of locking order number is to lock only the operation of the current 1 order, and if lock static variable, that is to lock all orders, will cause all orders to be queued, which is obviously unreasonable.

So can the lock (order number) method mentioned at the beginning of this article achieve the desired effect? Let's use some code to restore the usage scenario first.

If you ignore user information and other validations, the code looks pretty much like this:
For the lock keyword, MSDN includes information that can be found on Baidu, and it seems that it is recommended not to use lock(string), and the reason is the same. The following passage is taken from MSDN's advice on lock strings:

The lock("myLock") issue occurs because any other code using the same string in the process will share the same lock.
This sentence hides a huge mechanism, that is, "the same string".

What is "the same string"? See the code:


Are str1 and str2 the same string above? The answer is YES.

See again:


Are str1 and str2 above still the same string? The answer is NO.

Okay, let's go back to the issue of our order payment. In our code, lock(orderNumber), when the user accidentally clicks a few more times after swiping his hand, is the orderNumber entering this action the same string each time? The answer is NO. This is to say

The code that handles the order above doesn't actually act as a lock.

In fact, there are two types of string comparisons, see the code:


The first line of the above code outputs True, and the second line outputs False. I believe you understand what MSDN means by "the same string" without my explanation.

The best solution

Solutions for optimal locking strings:





Demo code:




In the website, sometimes a global variable may be used, this global variable, when multiple users access at the same time, may appear abnormal, at this time, we will set a global lock, but the disadvantage of this is that all accesses will wait in turn.

In some scenarios, for example, the same user can only comment once within 15 seconds, if the global lock is used, the comment function will be very slow to process when the number of users surges, which will greatly affect the user experience.

At this time,We can set the lock for each user individually, lock(string){...}, and the name of the lock can be defined as:Method name + user's IDIn this way, each user has an independent lock, and when judging the comment interval, it will not affect other users' comments.

(End)




Previous:MFC's LPSTR type writing
Next:openssl has obviously been updated, apache still prompts the old version?
Posted on 5/13/2022 2:27:17 PM |
So can the lock (order number) method mentioned at the beginning of this article achieve the desired effect? Let's use some code to restore the usage scenario first.
 Landlord| Posted on 8/12/2023 7:48:15 PM |
.NET/C# Lock Principle Monitor provides an in-depth explanation
https://www.itsvse.com/thread-9633-1-1.html
Posted on 1/7/2023 11:22:02 AM |
I also encountered it in my work practice, thanks to the author for this technical article
Posted on 7/3/2019 8:02:32 PM |
This is good stuff
Posted on 6/18/2021 2:24:43 PM |
What is shown on the page is that the code cannot be viewed
Posted on 6/29/2021 12:21:16 AM |
Look at the code
Posted on 6/29/2021 12:54:24 AM |
I can't see the code
Posted on 5/14/2022 10:06:46 AM |
Learn to learn
Posted on 5/28/2022 10:03:59 PM |
Very good, learned
Posted on 11/28/2022 9:46:47 PM |
Can asp.net be used?
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