Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build ASP.NET thread-safe collections

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to build ASP.NET thread-safe collections. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Most ASP.NET thread-safe collections have some fundamental flaws: although each operation is thread-safe, multiple operations cannot be combined. This means that some basic execution order, such as checking the number of elements in the stack before popping up the top elements, can be potentially dangerous. Although some API have managed to bind certain operations (such as the Coordination Data Structures of .NET 4), they tend to introduce ugly methods (such as TryDequeue).

The collections in .NET 1 try another way, exposing a SyncRoot property to the outside rather than locking it internally. Although SyncRoot is still the default mechanism for synchronizing objects, .NET 2 has abandoned the SyncRoot/Wrapper design pattern.

So how do you create a composite API that is available? Jared Parson believes that collections should not directly expose ASP.NET thread-safe API, and that all methods should belong to a temporary object that is created only when you lock the collection. This temporary object is the "key" of the collection, and only the holder of the key can obtain the contents of the collection.

The following example is a thread-safe queue for Jared Parsons:

Static void Example1 (ThreadSafeQueue queue) {using (var locked = queue.Lock ()) {if (locked.Count > 0) {var first = locked.Dequeue ();}

The object named locked itself is not ASP.NET thread-safe, but developers can only perform operations correctly in using code blocks. After following this simple rule, all the code in the development block is thread-safe. Jared explained:

Like most ASP.NET thread-safe designs, this code can be misused:

1. Continue to use ILockedQueue after it is destroyed. This practice should be prohibited, and the user's existing knowledge is generally sufficient to avoid this problem. In addition, some static checking tools, such as FxCop, recognize this as an error. We can also use a more stringent approach to prevent this from happening: add a disposed tag and check it in each method.

2. If the user retains a value (for example, Count) across multiple Lock statements, there may be misjudgments and assumptions about the state of the collection.

3. If the user does not terminate ILockedQueue correctly, the object will be locked by *. Fortunately, FxCop also recognizes this as an error for objects that implement IDisposable-- although it's not a perfectly safe mechanism.

4. It is impossible to determine whether the user will hold the ILockedQueue object for a long time. Although IDisposable generally means "short-term", it does not guarantee.

5. ILockedQueue is not thread safe. Although users do not normally hand over IDisposable objects to multiple threads, this is one of the situations that must be taken into account.

Thank you for reading! This is the end of this article on "how to build a thread-safe collection of ASP.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report