c#


C# List<T> indexer thread safety


Until recently, I had been under the assumption that setting an element of a List<T> via indexer is thread safe in the following context.
// Assumes destination.Count >= source.Count
static void Function<T,U>(List<T> source, Func<T,U> converter, List<U> destination)
{
Parallel.ForEach(Partitioner.Create(0, source.Count), range =>
{
for(int i = range.Item1; i < range.Item2; i++)
{
destination[i] = converter(source[i]);
}
});
}
Since List<T> stores its elements in an array internally and setting one by index shouldn't necessitate resizing, this seemed like a reasonable leap of faith. Looking at the implementation of List<T> in .NET Core however, it appears that the indexer's setter modifies some internal state (see below).
// Sets or Gets the element at the given index.
public T this[int index]
{
get
{
// Following trick can reduce the range check by one
if ((uint)index >= (uint)_size)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}
Contract.EndContractBlock();
return _items[index];
}
set
{
if ((uint)index >= (uint)_size)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}
Contract.EndContractBlock();
_items[index] = value;
_version++;
}
}
So should I assume that List<T> is not thread-safe even when each thread is only getting/setting elements from its own portion of the collection?
Have a read here:
https://msdn.microsoft.com/en-us/library/6sh2ey19.aspx#Anchor_10
To answer your question, no - as per the documentation, it's not guaranteed to be thread safe.
Even if the current implementation appeared to be thread safe (which it doesn't, anyway), it would still be a bad idea to make that assumption. Since the documentation explicitly says it's not thread safe - future versions may legally change the underlying implementation to no longer be thread safe and break any assumption you previously relied on.

Related Links

Linq Result Join all Rows Data to string
How to change the position of jquery dialog box into center?
Databinding to an ObservableCollection
C#. Clear console output from {X} line
What are the exceptional cases when using Thread Pool [closed]
How to read in some XML then split out various nodes/elements in .NET?
Object reference error for Outlook c#
Issues with the c# datarow
Html marquee with foreach
OpenGL support in windows 8 store Application C#
Search query returns none
Object reference not set to an instance of an object. error that I can't resolve [duplicate]
No overload for method '(Function Name)' takes '0' arguments
Extension method is not working,If method in nonstatic class? [duplicate]
quickfix/n error:'An Existing was forcibly closed the remote host at Quickfix.SocketInitiatorThread.Read()'
Changing the path/filename of SplashScreenImage.jpg in Windows Phone 8

Categories

HOME
itunesconnect
popover
postsharp
hex
cryengine
salesforce-lightning
css-selectors
meson-build
python-3.4
pyodbc
google-my-business-api
adroitlogic
exchange-transport-agents
greensock
facebook-marketing-api
openam
jquery-easyui
airbnb
aiml
scotty
android-wifi
formulas
coccinelle
beaker-notebook
adobe-captivate
utc
workflow-foundation
scripting-language
semantic-ui-react
jmsserializerbundle
tikz
mplab
jcr
availability
android-scrollview
headphones
flex3
maven-2
abide
scip
const
queryover
pygraphviz
motion
lex
gem-fury
ninja-forms
mixed-models
tapply
test-kitchen
risc
denodo
monaca
openpgp.js
hellosign
robotc
strtol
networkstream
require-handlebars
particle-system
oryx
nslocalizedstring
cascading
serial-communication
redquerybuilder
jain-sip
windows-phone-8-emulator
joomla3.3
borland-c++
callfire
p4python
beagleboard
nsq
crtdbg.h
cordova-3
yosemite
rda
sigma-grid-control
email-headers
node-amqp
jfilechooser
google-voice
randomaccessfile
caroufredsel
windows-scheduler
junction-table
ntdll
yii-components
blackberry-eclipse-plugin
mysql-connector
motorola-droid
unordered-set
mismatch
qvariant
hwnd
cinder
text-manipulation
numerical-computing
pixel-bender
wtsapi32
jgrowl
tracd

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile