Does ConcurrentDictionary TryGetValue use the if statement to make the contents of the if string?

If I have a ConcurrentDictionary and use TryGetValue in an if statement, does that make the contents of the contents of the if statement safe? Or should you still block in the if statement?

Example:

ConcurrentDictionary<Guid, Client> m_Clients; Client client; //Does this if make the contents within it thread-safe? if (m_Clients.TryGetValue(clientGUID, out client)) { //Users is a list. client.Users.Add(item); } 

or i need to do:

  ConcurrentDictionary<Guid, Client> m_Clients; Client client; //Does this if make the contents within it thread-safe? if (m_Clients.TryGetValue(clientGUID, out client)) { lock (client) { //Users is a list. client.Users.Add(item); } } 
+4
source share
2 answers

Yes, you have to lock inside the if statement, the only guarantee you get from the parallel dictionary is that its methods are to reduce the flow.

+4
source

The accepted answer may be misleading depending on your point of view and the amount of thread safety you are trying to achieve. This answer is aimed at people who stumble upon this question after learning about streaming and concurrency:

It is true that locking at the output of the dictionary search (Client object) makes some of the code flows safe, but only the code that accesses this restored object inside the lock. In this example, it is possible that another thread deletes this object from the dictionary after the current thread receives it. (Although there are no instructions between the extract and lock, other threads can still execute between them.) Then this code will add the Client object to the Users list, even if it is no longer in the parallel dictionary. This may cause an exception, synchronization, or race condition.

It depends on what the rest of the program does. But in the scenario that I am describing, it would be safer to put a lock around the entire dictionary search. And then a regular dictionary can be faster and easier than a parallel dictionary if you always block it when using it!

0
source

Source: https://habr.com/ru/post/1444659/


All Articles