1024programmer Asp.Net C# multi-threaded recording

C# multi-threaded recording

C# multi-threaded recording

In development, we often encounter different businesses accessing the same data source, and the execution flow of each business is a thread. At this time, if there are too many threads, multi-threading will occur and it is most likely to be encountered. The problem – concurrency.

What is concurrency?

To give a very classic example: in programs we often have to operate some objects, especially data in memory

For example, the current entry condition has determined that newModel is not empty, and sleep(10) is a relatively time-consuming operation. During this period, if newModel is left empty elsewhere, an exception will occur when sleep(10) ends. Sleep here is just an enlarged time. In actual business, most of this operation is at the millisecond or even microsecond level. It is difficult to find problems in this area without code review, which leads to various inexplicable exceptions in the project.

How Solving concurrency?

There are many online tutorials on concurrency solutions, mainly locking, and the combination of design mode and singleton mode to solve concurrency problems. I won’t give detailed examples here for the time being. Collection data in memory may be saved in list, array, queue, etc., but these are not thread-safe. Manual locking is required during multi-threaded operations. When the code is large, there is a probability of losing locks in some places. Or locking is repeated, or more seriously, a deadlock may occur.

Since there is thread non-safety, there must be thread safety. There are five main thread-safe collections (ConcurrentQueue, ConcurrentStack, ConcurrentBag, BlockingCollection, ConcurrentDictionary). They use lock-free technology and memory barriers to a certain extent than normal Using mutex locks has certain performance improvements. Here is a description of BlockingCollection.

BlockingCollection is similar to a blocking queue. Its most comfortable part is that it realizes the producer-consumer relationship, which is much better than ConcurrentQueue. At the same time, it also supports foreach and limits the maximum capacity.

BlockingCollection.Add If the production exceeds the specified capacity, BlockingCollection will block itself and stop production

If BlockingCollection.Take consumes too much and has no produced content, it will block itself until new produced content is added to avoid emptying and save performance.


This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/c-multi-threaded-recording/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索