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

Expressing a subset in binary
Force two decimal places on Decimal data type
Unable to save customer profile in Authorize.Net
How to determine whether a SteamVR_TrackedObject is a Vive Controller or a Vive Tracker
Closedxml find last row number c#
Is it possible to change the font family of a Literal in ASP.Net?
SonarQube false positive S1450 for VS 2017 RC C#
C# File.exists failing if process started by registry/run
contextual binding ninject c# MVC
Is it possible for IIS 8 to query a database according to an URL in order to make a redirect?
String Split to String Array without seperator
DbContext has been disposed when using nested LINQ
Why are some properties (e.g. IsSome and IsNone) for FSharpOption not visible from C#?
PlayerPrefs Equal to GameObject
How to programmatically modify the 2 values of the Adobe Distiller “AdobePDFOutputFolder” Registry Key?
Interacting with Wordpress website API from mobile app

Categories

HOME
typo3
packet
display
window.open
django-middleware
mailchimp
aggregation-framework
procmon
minimax
aws-sdk-cpp
facebook-sharer
distinct
resharper
http-status-codes
hdf5
android-notifications
webseal
ajaxcontroltoolkit
uipath
aspxgridview
gulp-jshint
schedule
mplab
log4net-configuration
pymunk
form-fields
iterm2
server-side-swift
pyexcel
autodesk-data-management
text-extraction
html-entities
taleo
jenkins-2
stream-socket-client
nao-robot
multivalue
perldoc
fluent-nhibernate-mapping
androiddesignsupport
try-catch-finally
tsc
mate-desktop
qtruby
audiorecord
boost-propertytree
android-progressbar
abstract
methodology
nonblocking
yfiles
encode
php-gd
log4cxx
tess4j
xml-simple
opennebula
tidesdk
asp.net-2.0
pymol
angular-file-upload
opencobol
rssi
iwork
xtify
surfaceview
sygic
pechkin
friendly-url
exi
nx
architectural-patterns
setcookie
yii-components
wpml
netzke
jquery-1.8
php-gettext
jquery-ui-map
mvcmailer
inserthtml
joomla1.7
utm
nbehave
norton
conditional-operator
chromebug
dongle
silverlight-2.0
for-xml
lts
user-preferences
disk-based
managed-code
stackless
scalable

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App