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

Reusable Included Layouts
Choosing right representation of entity [closed]
Entity framework one-to-many relationship - how to rename foreign key name when it is named Id?
Searching Tri Data Structure
How to see how many thread is working to complete my task TPL?
How can I Store this pointer in a buffer in C#?
How to dispose of DbContext in WebApi?
Use namedPipe or UDP?
How to call my int array class to ad my dropdownlist [closed]
Track a stick with blob tracking or by color? Using Kinect, opencv and C#
WinRT RemovableDevices: Music folder: Cannot create items within this StorageFolder
Deploying application using SQL CE 4.0 to work both in 32 and 64 bit PC
Visible rows in DataGrid is off by 1 (counted using ContainerFromItem)
ASP.NET was the checkbox was checked in repeater?
UsedRange.RowCount is not updated after an insert
how to compare three strings using strig.compare in c#.net? [closed]

Categories

HOME
asp.net
google-cloud-pubsub
locale
packet
formatting
glsl
itext7
psexec
opacity
ejb-3.0
activejdbc
comma
fastreport
repeater
webseal
responsive
pylint
project-structure
spring-webflow
react-jsx
google-drive-android-api
chartist.js
traveling-salesman
ballerinalang
typeorm
units-of-measurement
2checkout
deferred
andengine
boost-log
textview
jql
odp.net
certbot
campaign-monitor
motherboard
multi-user
rythm
origami
nshttpurlresponse
html-entities
angular-fullstack
anonymous-function
easyquery
stream-socket-client
jsfl
kendo-scheduler
overhead
stripes
gevent
settext
virtualenvwrapper
bytearray
jmh
clique
ewam
web-performance
app-data
android-facebook
magnet-uri
sdkman
notice
identifying
first-order-logic
eaaccessory
eggplant
transparentproxy
page-refresh
radgrid
google-experiments
drools-guvnor
sync
tigase
powershell-v1.0
indic
fscanf
drawable
rpt
step
atg-dynamo
opencobol
nodeunit
iwork
mixradio
logo-lang
complete
xcode5.1
symbian3
antixsslibrary
motordriver
parsec
automapper-2
file-processing
n2
mysql-logic

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