Home > Net >  The List thread safety problem
The List thread safety problem

Time:11-18

List saved several objects, through the long connection Socket multithreaded concurrent heartbeat package update object state (each object a Socket, update), often need to Find the.findall or for loop, such as modified operation object, occasionally have a List of the ADD and Remove operations, which place need the lock?

CodePudding user response:

Multithreaded modified the same object of different attributes

CodePudding user response:

The List is not suitable for this kind of situation,
With the Dictionary data structures, such as the ConcurrentDictionary,

CodePudding user response:

refer to the second floor github_36000833 response:
the List is not suitable for this kind of situation,
Use Dictionary data structures, such as the ConcurrentDictionary,


If can only use the List

CodePudding user response:

To receive updates in the queue ConcurrentQueue again for processing

CodePudding user response:

In the list, the basic lock, the Find is also in a for loop

CodePudding user response:

Deletion, every action to lock

CodePudding user response:

System. Collections. Concurrent there are several thread-safe Collections

CodePudding user response:

From your first qualified, directly from your original requirements about

Heart rate corresponding to the session -- -- -- -- -- -- -- -- in the IT world of passage session with what? At first glance, of course, is the cache

Your request can be a Cache, Microsoft MemoryCache to you is thread-safe, you can also save some operations (such as prolong survival time, automatically remove expired)

CodePudding user response:

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/lock-statement

CodePudding user response:

MSDN already recommend you thread-safe collection
? ConcurrentQueue
? ConcurrentStack
? Load-balanced across:
? BlockingCollection:
? The ConcurrentDictionary

You choose good,

As for must use the list, then the Lock

CodePudding user response:

refer to the original poster hhhhha123 response:
often need to Find the.findall or for loop modified operation object, such as the occasional List of ADD, Remove, such as operation, which place need the lock?


It is these operations require the lock, such as
 lock (myList) 
Return the.findall (myList,... );
the underlying operations require the whole lock,

CodePudding user response:

reference 1st floor hhhhha123 response:
multithreaded modified the same object of different attribute


Modify the queue attributes of the object one, not the lock queue, but found the lock objects,

CodePudding user response:

refer to the original poster hhhhha123 response:
List saved several objects, through the long connection Socket multithreaded concurrent heartbeat package update object state (each object a Socket, update), often need to Find the.findall or for loop modified operation object, such as the occasional List of ADD, Remove, such as operation, which place need the lock?


All need atomic operations, coupled with the lock

CodePudding user response:

Lock is a performance bottleneck, so usually as far as possible for the "details" synchronization, not too much from the top to the deadlock of large objects, thus concurrent multi-threaded design more embodies a way of "philosophy", also is to pay attention to the way of demonstration, through the system self-test to determine the "relative" is a concept, rather than to judge based on the alien concept of the so-called absolute right and wrong concepts, usually we will write some tests, such as
 Task [] tasks=... ; 
Await Task. WhenAll (tasks);
The Debug. Assert (checkResults (), "system of concurrent abnormal operation");
and is to be repeated hundreds of times a day, for example you can make your lunch hour dozens of test cases to repeat instantiation, and parallel run tens of thousands of tests, and then see if the final assertion can find is one over one hundred million of the data inconsistency,

What programming is not depend on others to instill "theory" and decided to design good or bad, want to rely on oneself write tests for a long time,

CodePudding user response:

Use the ConcurrentDictionary

CodePudding user response:

reference 11 floor interacting in a professional developer response:
Quote: refer to the original poster hhhhha123 response:
often need to Find the.findall or for loop modified operation object, such as the occasional List of ADD, Remove, such as operation, which place need the lock?


It is these operations require the lock, such as
 lock (myList) 
Return the.findall (myList,... );
the underlying operating need whole lock,



there is a problem, that is, if the lock, in order to do performance will produce errors and consequences? (can accept dirty reads, basic no operation when the same object at the same time, the probability of ADDRemove also is very small, if error try, can catch catch to also go, as long as you don't get system collapse)

CodePudding user response:

Thank you

CodePudding user response:

reference 16 floor hhhhha123 response:
... Is if for performance...


List data structure of the Find, Remove complexity is O (n), and Dictionary data structure complexity is O (1),
Too simple said, when you have 2000 connections, Find and Remove operation Dictionary algorithm is 2000 times of high efficiency,

CodePudding user response:

From a performance overhead, the lock itself quickly, when there is a race, the performance is' substantially 'decline (the' big 'here is relatively not race lock),

Such as the List to remove an element, the List need to target element removed, advancing a location, and all subsequent data
For such operations, the process of locking will be relatively long, it is easier to cause a race (or multiple operating scrambled to lock, and compete with each other).

CodePudding user response:

Do you want to achieve the effect of the data synchronization, there will not be able to delete can not find the object, or Null
  •  Tags:  
  • C#
  • Related