Quick开发者_运维百科 multithreading question...
I have a single dictionary object in memory containing about 20,000 items. If I have multiple threads attempting to read from this object, would you expect this to create a bottleneck?
NET 3.5 . Dictionary object would be read only
Dictionary is ReadOnly so I'm not concerned about read/writes, only performance.
It won't create a bottleneck but a dictionary is not thread safe so you might not get the expected results. In .NET 4.0 you could use ConcurrentDictionary<TKey, TValue>
for this purpose.
If it's a readonly dictionary then it's probably safe to have concurrent readers, just make sure that you fill this dictionary with data in a static constructor to make sure that writing doesn't interfere with reading and it takes place before any thread tries to read.
If all you're doing is reading and not doing any writes to the dictionary then you should be fine.
Thread safety applies more to mutation in this case, so if you're not mutating your dictionary in any way you're good.
Depends on what locking and writing is going on.
If you do not write concurrently, I think it is safe to read w/o locking and then there is no real bottleneck. But there are no clear specs on this.
If you do write (and thus lock), it depends on what else is going on.
No bottleneck that I can see from reading,
but I have my doubts about using the Enumerator.
I have not proven this yet, but instinct tells me multi threaded apps, using the Dictionary Key enumerator MIGHT cause some problems...
If the intention is that you initialize the dictionary and then it remains read-only, then using a plain dictionary is fine and will have no bottleneck as it is not thread-safe and so there is no thread synchronization overhead.
If you are indeed supporting a read-only pattern, then I would suggest encapsulating your dictionary in a container type class that only supports read operations. Your clients can then not subvert your design intent by "writing" to the dictionary.
use this: System.Threading.ReaderWriterLock
.
create new the dictionary.
lock the "global" location.
assign it.
unlock it.
if it just a datasource try to use the ReadOnly-Property
精彩评论