C# - 从字典中获得时锁定资源 [英] C# - Locking a resource when obtained from dictionary

查看:167
本文介绍了C# - 从字典中获得时锁定资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跟踪对象词典(ClientObject)。无论是字典和ClientObject的由多个线程访问。当我修改或本词典在阅读任何对象,我得到一个读或写在使用ReaderWriterLockSlim(rwl_clients)字典锁,然后获得实际的对象上的排他锁。



我只是想知道如果我正确地使用这些.NET线程设施



例如:

  rwl_clients.EnterReadLock(); 
ClientObject clobj;
如果
回报率(m_clients.TryGetValue(键,出clobj)!);
rwl_clients.ExitReadLock();

的someMethod(clobj);



的someMethod(ClientObject clobj)会做一些这样的:

 锁(clobj){

///阅读clobj

/写操作}

是否获取和锁定在一个线程的字典中的值(ClientObject)意味着其他线程将尊重锁?换句话说,确实.NET看到字典作为一个单一的资源价值(而不是复制),因此将尊重所有线程该资源的锁?



还有一个问题,从本字典中的资源时 - 我应该在执行删除之前锁定()



例如:

  rwl_clients.EnterWriteLock(); 
ClientObject clobj;
如果(m_clients.TryGetValue(键,出clobj)){
锁(clobj){
m_clients.Remove(键);
}
}
rwl_clients.ExitWriteLock();



我已经学会从这个网站这么多,明白任何反应!
感谢。


解决方案

是否获取和从字典锁定值(ClientObject)在一个线程意味着其他线程将尊重锁?换句话说,确实.NET看到字典作为一个单一的资源价值(而不是复制),因此将尊重所有线程该资源的锁?




这取决于类型 - 如果引用类型则是,如果一个值类型没有。这也是为什么你应该永远,永远锁定值类型,因为值类型将被装箱和任何后续尝试锁定该值实际上将获得不同的对象上的锁。




还有一个问题,从本字典中的资源时 - 我应该执行删除()



$之前锁定; b $ b

是的,你应该是变异对象的状态的任何操作之前锁定



作为一个方面说明 - 你确定此设置是最好的解决问题了吗?跨线程共享可变对象往往造成更多的问题,那么他们解决。


I have a Dictionary that tracks objects (ClientObject). Both the dictionary and ClientObject's are accessed by multiple threads. When I modify or read any object in this dictionary, I obtain a read or write lock on the dictionary using ReaderWriterLockSlim (rwl_clients) then obtain an exclusive lock on the actual object.

I just wanted to know if I am using these .net threading facilities correctly

Example:

rwl_clients.EnterReadLock();
ClientObject clobj;
if(!m_clients.TryGetValue(key, out clobj))
    return;
rwl_clients.ExitReadLock();

SomeMethod(clobj);

SomeMethod(ClientObject clobj) would do something like:

lock(clobj) {

/// Read / Write operations on clobj

}

Does getting and locking a value (ClientObject) from the dictionary in one thread mean that other threads will respect that lock? In other words, does .net see a value in the dictionary as a single resource (and not a copy) and will therefore respect a lock on that resource in all threads?

One more question, when removing a resource from the dictionary - should I lock it before performing Remove()

Example:

rwl_clients.EnterWriteLock();
ClientObject clobj;
if(m_clients.TryGetValue(key, out clobj)) {
     lock(clobj) {
          m_clients.Remove(key);
     }
}
rwl_clients.ExitWriteLock();

I have learned so much from this site and appreciate any responses! Thanks.

解决方案

Does getting and locking a value (ClientObject) from the dictionary in one thread mean that other threads will respect that lock? In other words, does .net see a value in the dictionary as a single resource (and not a copy) and will therefore respect a lock on that resource in all threads?

It depends on the type - if a reference type then yes, if a value type no. This is also why you should never, ever lock on a value type since the value type will be boxed and any subsequent attempts to lock on that value will actually acquire a lock on a different object.

One more question, when removing a resource from the dictionary - should I lock it before performing Remove()

Yes, you should lock before any operation that mutates the state of the object.

As a side note - are you sure that this setup is the best possible solution to your problem? Mutable objects shared across threads tend to create more problems then they solve.

这篇关于C# - 从字典中获得时锁定资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆