Reputation:
If you are working with a large List (lets say 1GB in size), and it needs to be resized to make room for additional items. Is it possible to read/write to that list while it is being resized, or is the call going to be bocked until the operation is finished?
Upvotes: 0
Views: 219
Reputation: 1499770
You say "1GB in size" but what does this actually mean? Bear in mind that if you're working with a reference type, all that will be in the list itself is a bunch of references - so the bulk of the memory taken up by "a list and all its elements" is going to be in the elements themselves, unless you have a lot of duplicate references.
Now, as to your question itself - List<T>
isn't threadsafe. You shouldn't be trying to read it while it's being modified in another thread. If you need to work with a list in multiple threads, with some of them modifying it, you should use locking to make sure that only one thread accesses it at time (or perhaps multiple threads reading, but not writing).
Upvotes: 5
Reputation: 32950
If you are working with an extremely large list, I highly recommend you preallocate space before you use it so you can avoid having it dynamically grow a lot. If you expect to use a gig, figure out a capacity that would handle just over a gig, and create the list with that capacity. That should eliminate the need to expand at all, and your problem will go away.
If you need concurrent capabilities in your lists, I would look into the parallel extensions to .NET. There are concurrent collections that allow multi-threaded access. I am not exactly sure if they allow concurrent access while resizing, but it is worth a look. Parallel extensions will be included as part of .NET 4.0 as well.
Upvotes: 0