Is it safe to access multiple indexes of a C# List concurrently?Parallel.ForEach on List<Object> Thread SafetyPossible to modify a List while iterating through it?Can a List<t> be accessed by multiple threads?Remove duplicates from a List<T> in C#Finding the index of an item given a list containing it in PythonHow do I clone a generic list in C#?Accessing the index in 'for' loops?In C#, what is the difference between public, private, protected, and having no access modifier?How to remove an element from a list by index?ArrayList vs List<> in C#C# List<string> to string with delimiterIntelligent way of removing items from a List<T> while enumerating in C#Getting a list item by index
If the railway suggests a 5-min connection window for changing trains in the Netherlands, does that mean it's definitely doable?
How to memorize multiple pieces in only 20 days?
Why does the U.S. tolerate foreign influence from Saudi Arabia and Israel on its domestic policies while not tolerating that from China or Russia?
Is purchasing foreign currency before going abroad a losing proposition?
Get ids only where one id is null and other isn't
Why weren't bootable game disks ever common on the IBM PC?
As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?
Can I call 112 to check a police officer's identity in the Czech Republic?
Why were Er and Onan punished if they were under 20?
How to find the shape parameters of of a beta distribution given the position of two quantiles?
Is it worth upgrading to 28mm tyres from 25 for a 250 mile ride?
How to tell someone I'd like to become friends without causing them to think I'm romantically interested in them?
Did any of the founding fathers anticipate Lysander Spooner's criticism of the constitution?
What does "un pareil soin" mean here?
The monorail explodes before I can get on it
3D print appears to print very weak walls in long print
Do you know your 'KVZ's?
Should I intentionally omit previous work experience when applying for jobs?
Print the last, middle and first character of your code
Cops: The Hidden OEIS Substring
For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?
Who has taken "my" Managed package namespace? Can we find out?
Are randomly-generated passwords starting with "a" less secure?
During copyediting, journal disagrees about spelling of paper's main topic
Is it safe to access multiple indexes of a C# List concurrently?
Parallel.ForEach on List<Object> Thread SafetyPossible to modify a List while iterating through it?Can a List<t> be accessed by multiple threads?Remove duplicates from a List<T> in C#Finding the index of an item given a list containing it in PythonHow do I clone a generic list in C#?Accessing the index in 'for' loops?In C#, what is the difference between public, private, protected, and having no access modifier?How to remove an element from a list by index?ArrayList vs List<> in C#C# List<string> to string with delimiterIntelligent way of removing items from a List<T> while enumerating in C#Getting a list item by index
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've got a list full of structs, which I want to iterate through and alter concurrently.
The code is conceptually as follows:
Parallel.For(0, pointsList.Count(), i=> pointsList[i] = DoThing(pointsList[i]));
I'm neither adding to nor removing from the list, only accessing and mutating its items.
I imagine this is fine, but thought I should check: is this OK as is, or do I need to use Lock somewhere for fear that I mess up the list object??
c# multithreading list
|
show 2 more comments
I've got a list full of structs, which I want to iterate through and alter concurrently.
The code is conceptually as follows:
Parallel.For(0, pointsList.Count(), i=> pointsList[i] = DoThing(pointsList[i]));
I'm neither adding to nor removing from the list, only accessing and mutating its items.
I imagine this is fine, but thought I should check: is this OK as is, or do I need to use Lock somewhere for fear that I mess up the list object??
c# multithreading list
It is safe to perform multiple read operations on aList<T>, but issues can occur if the collection is modified while it's being read.
– PetSerAl
Mar 26 at 3:23
1
Possible duplicate of Can a List<t> be accessed by multiple threads? or Parallel.ForEach on List<Object> Thread Safety
– BACON
Mar 26 at 3:23
Hi @PetSerAl. This is indeed a relevant link, which I have already looked up. However, the page was not explicit though about exactly which operations would cause which issues. For example, I would expect adding, inserting, removing, and clearing a list all to cause issues, I would possibly think that simply accessing list elements is fine
– Jack
Mar 26 at 3:28
Hi @BACON, thanks for the response: this question is different to the one you linked, which is premised on the idea that "the list will be locked during a changes", whereas I'm asking if this is really neccesary.
– Jack
Mar 26 at 3:31
The link from @PetSerAl tells you all you need to know. Reading the list is fine, modifying it is not.
– DavidG
Mar 26 at 3:36
|
show 2 more comments
I've got a list full of structs, which I want to iterate through and alter concurrently.
The code is conceptually as follows:
Parallel.For(0, pointsList.Count(), i=> pointsList[i] = DoThing(pointsList[i]));
I'm neither adding to nor removing from the list, only accessing and mutating its items.
I imagine this is fine, but thought I should check: is this OK as is, or do I need to use Lock somewhere for fear that I mess up the list object??
c# multithreading list
I've got a list full of structs, which I want to iterate through and alter concurrently.
The code is conceptually as follows:
Parallel.For(0, pointsList.Count(), i=> pointsList[i] = DoThing(pointsList[i]));
I'm neither adding to nor removing from the list, only accessing and mutating its items.
I imagine this is fine, but thought I should check: is this OK as is, or do I need to use Lock somewhere for fear that I mess up the list object??
c# multithreading list
c# multithreading list
edited Mar 26 at 4:08
Jack
asked Mar 26 at 3:19
JackJack
1139 bronze badges
1139 bronze badges
It is safe to perform multiple read operations on aList<T>, but issues can occur if the collection is modified while it's being read.
– PetSerAl
Mar 26 at 3:23
1
Possible duplicate of Can a List<t> be accessed by multiple threads? or Parallel.ForEach on List<Object> Thread Safety
– BACON
Mar 26 at 3:23
Hi @PetSerAl. This is indeed a relevant link, which I have already looked up. However, the page was not explicit though about exactly which operations would cause which issues. For example, I would expect adding, inserting, removing, and clearing a list all to cause issues, I would possibly think that simply accessing list elements is fine
– Jack
Mar 26 at 3:28
Hi @BACON, thanks for the response: this question is different to the one you linked, which is premised on the idea that "the list will be locked during a changes", whereas I'm asking if this is really neccesary.
– Jack
Mar 26 at 3:31
The link from @PetSerAl tells you all you need to know. Reading the list is fine, modifying it is not.
– DavidG
Mar 26 at 3:36
|
show 2 more comments
It is safe to perform multiple read operations on aList<T>, but issues can occur if the collection is modified while it's being read.
– PetSerAl
Mar 26 at 3:23
1
Possible duplicate of Can a List<t> be accessed by multiple threads? or Parallel.ForEach on List<Object> Thread Safety
– BACON
Mar 26 at 3:23
Hi @PetSerAl. This is indeed a relevant link, which I have already looked up. However, the page was not explicit though about exactly which operations would cause which issues. For example, I would expect adding, inserting, removing, and clearing a list all to cause issues, I would possibly think that simply accessing list elements is fine
– Jack
Mar 26 at 3:28
Hi @BACON, thanks for the response: this question is different to the one you linked, which is premised on the idea that "the list will be locked during a changes", whereas I'm asking if this is really neccesary.
– Jack
Mar 26 at 3:31
The link from @PetSerAl tells you all you need to know. Reading the list is fine, modifying it is not.
– DavidG
Mar 26 at 3:36
It is safe to perform multiple read operations on a
List<T>, but issues can occur if the collection is modified while it's being read.– PetSerAl
Mar 26 at 3:23
It is safe to perform multiple read operations on a
List<T>, but issues can occur if the collection is modified while it's being read.– PetSerAl
Mar 26 at 3:23
1
1
Possible duplicate of Can a List<t> be accessed by multiple threads? or Parallel.ForEach on List<Object> Thread Safety
– BACON
Mar 26 at 3:23
Possible duplicate of Can a List<t> be accessed by multiple threads? or Parallel.ForEach on List<Object> Thread Safety
– BACON
Mar 26 at 3:23
Hi @PetSerAl. This is indeed a relevant link, which I have already looked up. However, the page was not explicit though about exactly which operations would cause which issues. For example, I would expect adding, inserting, removing, and clearing a list all to cause issues, I would possibly think that simply accessing list elements is fine
– Jack
Mar 26 at 3:28
Hi @PetSerAl. This is indeed a relevant link, which I have already looked up. However, the page was not explicit though about exactly which operations would cause which issues. For example, I would expect adding, inserting, removing, and clearing a list all to cause issues, I would possibly think that simply accessing list elements is fine
– Jack
Mar 26 at 3:28
Hi @BACON, thanks for the response: this question is different to the one you linked, which is premised on the idea that "the list will be locked during a changes", whereas I'm asking if this is really neccesary.
– Jack
Mar 26 at 3:31
Hi @BACON, thanks for the response: this question is different to the one you linked, which is premised on the idea that "the list will be locked during a changes", whereas I'm asking if this is really neccesary.
– Jack
Mar 26 at 3:31
The link from @PetSerAl tells you all you need to know. Reading the list is fine, modifying it is not.
– DavidG
Mar 26 at 3:36
The link from @PetSerAl tells you all you need to know. Reading the list is fine, modifying it is not.
– DavidG
Mar 26 at 3:36
|
show 2 more comments
1 Answer
1
active
oldest
votes
It is not guaranteed to be safe. Changing an element in the list increments the list's private _version field, which is how various threads can tell if the list has been modified. If any other thread attempts to enumerate the list or uses a method like List<T>.ForEach() then there could potentially be an issue. It sounds like it would be extremely rare, which isn't necessarily a good thing-- in fact, it makes for the most maddening type of defect.
If you want to be safe, create a new list instead of changing the existing one. This is a common "functional" approach to threading issues that avoids locks.
var newList = pointsList.AsParallel().Select( item => DoThing(item) );
When you're done, you can always replace the old list if you want.
pointsList = newList.ToList();
1
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
1
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
1
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. Thesetindexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using aforloop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.
– BACON
Mar 26 at 19:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349362%2fis-it-safe-to-access-multiple-indexes-of-a-c-sharp-listt-concurrently%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is not guaranteed to be safe. Changing an element in the list increments the list's private _version field, which is how various threads can tell if the list has been modified. If any other thread attempts to enumerate the list or uses a method like List<T>.ForEach() then there could potentially be an issue. It sounds like it would be extremely rare, which isn't necessarily a good thing-- in fact, it makes for the most maddening type of defect.
If you want to be safe, create a new list instead of changing the existing one. This is a common "functional" approach to threading issues that avoids locks.
var newList = pointsList.AsParallel().Select( item => DoThing(item) );
When you're done, you can always replace the old list if you want.
pointsList = newList.ToList();
1
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
1
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
1
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. Thesetindexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using aforloop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.
– BACON
Mar 26 at 19:38
add a comment |
It is not guaranteed to be safe. Changing an element in the list increments the list's private _version field, which is how various threads can tell if the list has been modified. If any other thread attempts to enumerate the list or uses a method like List<T>.ForEach() then there could potentially be an issue. It sounds like it would be extremely rare, which isn't necessarily a good thing-- in fact, it makes for the most maddening type of defect.
If you want to be safe, create a new list instead of changing the existing one. This is a common "functional" approach to threading issues that avoids locks.
var newList = pointsList.AsParallel().Select( item => DoThing(item) );
When you're done, you can always replace the old list if you want.
pointsList = newList.ToList();
1
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
1
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
1
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. Thesetindexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using aforloop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.
– BACON
Mar 26 at 19:38
add a comment |
It is not guaranteed to be safe. Changing an element in the list increments the list's private _version field, which is how various threads can tell if the list has been modified. If any other thread attempts to enumerate the list or uses a method like List<T>.ForEach() then there could potentially be an issue. It sounds like it would be extremely rare, which isn't necessarily a good thing-- in fact, it makes for the most maddening type of defect.
If you want to be safe, create a new list instead of changing the existing one. This is a common "functional" approach to threading issues that avoids locks.
var newList = pointsList.AsParallel().Select( item => DoThing(item) );
When you're done, you can always replace the old list if you want.
pointsList = newList.ToList();
It is not guaranteed to be safe. Changing an element in the list increments the list's private _version field, which is how various threads can tell if the list has been modified. If any other thread attempts to enumerate the list or uses a method like List<T>.ForEach() then there could potentially be an issue. It sounds like it would be extremely rare, which isn't necessarily a good thing-- in fact, it makes for the most maddening type of defect.
If you want to be safe, create a new list instead of changing the existing one. This is a common "functional" approach to threading issues that avoids locks.
var newList = pointsList.AsParallel().Select( item => DoThing(item) );
When you're done, you can always replace the old list if you want.
pointsList = newList.ToList();
edited Mar 26 at 3:45
answered Mar 26 at 3:42
John WuJohn Wu
33.5k4 gold badges27 silver badges56 bronze badges
33.5k4 gold badges27 silver badges56 bronze badges
1
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
1
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
1
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. Thesetindexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using aforloop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.
– BACON
Mar 26 at 19:38
add a comment |
1
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
1
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
1
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. Thesetindexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using aforloop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.
– BACON
Mar 26 at 19:38
1
1
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
Good point on it bumping the version number. I didn't expect it to do that but it does. The relevant source code is here / here / here.
– BACON
Mar 26 at 3:45
1
1
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Oh yeah, it looks like incrementing _version in parallel could result in a dirty write. This is only relevant for when the list is being altered or used as an IEnumerable however, neither of which I am doing. Furthermore, if I did perform any of the above operations on pointsList later on, the residual value inside _version wouldn't actually have any detrimental affect. However, the thought process here is complex enough I think I'd want to save the next guy from having to think about it, and so I think I'll avoid assigning to the list in parallel, as suggested.
– Jack
Mar 26 at 4:00
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
Thanks for the answer John. .AsParallel() is a nifty little extension method.
– Jack
Mar 26 at 4:01
1
1
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. The
set indexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using a for loop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.– BACON
Mar 26 at 19:38
@Jack After further consideration, I reached the same conclusion as your 2nd sentence. The
set indexer invalidates any enumeration operations...but why does that matter if you know there are none? It's like using a for loop to remove list items: no, you can't simultaneously access the list on another thread, but having that limitation doesn't mean that loop is "unsafe". I suppose it boils down to if you know the list won't be accessed in problematic ways then there's no problem, otherwise taking the cautious approach isn't a bad thing, either.– BACON
Mar 26 at 19:38
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349362%2fis-it-safe-to-access-multiple-indexes-of-a-c-sharp-listt-concurrently%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It is safe to perform multiple read operations on a
List<T>, but issues can occur if the collection is modified while it's being read.– PetSerAl
Mar 26 at 3:23
1
Possible duplicate of Can a List<t> be accessed by multiple threads? or Parallel.ForEach on List<Object> Thread Safety
– BACON
Mar 26 at 3:23
Hi @PetSerAl. This is indeed a relevant link, which I have already looked up. However, the page was not explicit though about exactly which operations would cause which issues. For example, I would expect adding, inserting, removing, and clearing a list all to cause issues, I would possibly think that simply accessing list elements is fine
– Jack
Mar 26 at 3:28
Hi @BACON, thanks for the response: this question is different to the one you linked, which is premised on the idea that "the list will be locked during a changes", whereas I'm asking if this is really neccesary.
– Jack
Mar 26 at 3:31
The link from @PetSerAl tells you all you need to know. Reading the list is fine, modifying it is not.
– DavidG
Mar 26 at 3:36