如果我有ConcurrentDictionary,并且在if语句中使用TryGetValue,这是否会使if语句的内容线程安全?还是必须仍然锁定在if语句中?
例:
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);
}
Run Code Online (Sandbox Code Playgroud)
还是我必须做:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6716 次 |
| 最近记录: |