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

Connecting Unity3D to SQL [CodePage 1256 not supported]
Using Azure Active Directory - one application to login locally and when published
The best overloaded Method has some invalid arguments and Cannot convert type
Detecting a PowerShell Switch
Re-prioritize items in a rabbitMq priority queue
c# DataGridView get Cells Value from Selected Row
How do I fix CS0433 errors in ASP.NET 5?
excel interop variable range
Event logger in Windows 10 Universal Apps
How to encrypt a file in .net and decrypt in android
Windows Universal App 8.1 Custom User Control
Configuration.Save on a network drive
xElement query equivalent for sql like
Convert string to an array of strings on the fly
How to get several sections of a string with a single regex using C#
Mocking Entity Framework 6 ObjectResult with Moq

Categories

HOME
kendo-ui-angular2
botframework
ldap
highmaps
itext7
mailchimp
multipartform-data
richtextbox
atlassian-fisheye
flowtype
kohana
google-api-oauth
ionic-native
box-api
elastalert
suitecrm
vichuploaderbundle
minimum
bookmarklet
pylint
template10
hl7-fhir
custom-component
ajaxcontroltoolkit
smali
fastboot
email-client
procedural-generation
rvm
ps
sphinx4
overflow
log4net-configuration
zimbra
bootcamp
minitest
mkdir
matrix-multiplication
stencyl
mockserver
google-chrome-console
java-5
synchronized
azure-data-catalog
mef
activeandroid
jscoverage
gige-sdk
openpgp.js
sp
try-catch-finally
directinput
class-design
netlink
hotkeys
hexagonal-tiles
euro
android-networking
transparentproxy
random-access
sync
livescript
im4java
git-subtree
php-amqplib
google-oauth-java-client
scidb
mencoder
nsnumber
xrandr
maven-webstart-plugin
leap-year
select2-rails
exi
pudb
django-tinymce
linqdatasource
junit-rule
certificate-revocation
mysql-connector
joomla1.7
copyfile
cgaffinetransform
qvariant
jexcelapi
service-layer
cryptolicensing
wtsapi32
expander

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App