Discussion:
Are DataViews thread safe?
(too old to reply)
William Sullivan
2005-08-25 18:31:02 UTC
Permalink
I know DataSets are threadsafe, but what about DataViews from the DefaultView
accessor? Here's the situation: ASP.NET application; DataSets in the Cache;
Each session gets a DataView from each DataSet via DefaultView, applies a
filter to that DataView, and displays the result to the user. From what I
understand, if two threads grab the same DefaultView from the same DataSet,
and each applies a filter to it, the data displayed to both users will be the
same (it will be what is allowed by the last filter applied). Is this true?
Right now, when I grab the DefaultView, I Copy() the DataTable before. The
DataTables might contain a million or so entries, so this could bog stuff
down in the extreme case...
W.G. Ryan eMVP
2005-08-26 23:15:18 UTC
Permalink
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadataviewclasstopic.asp

Thread Safety
This type is safe for multithreaded read operations. You must synchronize
any write operations.
Post by William Sullivan
I know DataSets are threadsafe, but what about DataViews from the DefaultView
accessor? Here's the situation: ASP.NET application; DataSets in the Cache;
Each session gets a DataView from each DataSet via DefaultView, applies a
filter to that DataView, and displays the result to the user. From what I
understand, if two threads grab the same DefaultView from the same DataSet,
and each applies a filter to it, the data displayed to both users will be the
same (it will be what is allowed by the last filter applied). Is this true?
Right now, when I grab the DefaultView, I Copy() the DataTable before.
The
DataTables might contain a million or so entries, so this could bog stuff
down in the extreme case...
Miha Markic [MVP C#]
2005-08-29 10:34:27 UTC
Permalink
Hi Willima,
Post by William Sullivan
I know DataSets are threadsafe,
Are they? I don't think so.

but what about DataViews from the DefaultView
Post by William Sullivan
accessor? Here's the situation: ASP.NET application; DataSets in the Cache;
Each session gets a DataView from each DataSet via DefaultView, applies a
filter to that DataView, and displays the result to the user. From what I
understand, if two threads grab the same DefaultView from the same DataSet,
and each applies a filter to it, the data displayed to both users will be the
same (it will be what is allowed by the last filter applied). Is this true?
No.
Post by William Sullivan
Right now, when I grab the DefaultView, I Copy() the DataTable before.
The
DataTables might contain a million or so entries, so this could bog stuff
down in the extreme case...
You should synchronize the access using one of the synchronization
mechanisms such as Monitor class.
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info
William Sullivan
2005-08-29 12:42:46 UTC
Permalink
Post by Miha Markic [MVP C#]
Post by William Sullivan
I know DataSets are threadsafe,
Are they? I don't think so.
Somewhat safe. Reading, not writing. In my case, since I only write in one
thread, it is safe.
Post by Miha Markic [MVP C#]
but what about DataViews from the DefaultView
Post by William Sullivan
accessor? Here's the situation: ASP.NET application; DataSets in the Cache;
Each session gets a DataView from each DataSet via DefaultView, applies a
filter to that DataView, and displays the result to the user. From what I
understand, if two threads grab the same DefaultView from the same DataSet,
and each applies a filter to it, the data displayed to both users will be the
same (it will be what is allowed by the last filter applied). Is this true?
No.
So, DefaultView doesn't return the same object every time it's called? If
it does, then if two threads give it different filters at the same time, the
last filter applied would be shown to both users. If it returns a different
object each time its called, then this would obviously be the case. So which
is it; same object, or different object?
Post by Miha Markic [MVP C#]
You should synchronize the access using one of the synchronization
mechanisms such as Monitor class.
I am not writing to the datasets once they are cached, so I don't think I'll
need to do this.
Nigel Norris
2005-08-29 13:29:42 UTC
Permalink
Post by William Sullivan
Post by Miha Markic [MVP C#]
Post by William Sullivan
I know DataSets are threadsafe,
Are they? I don't think so.
Somewhat safe. Reading, not writing. In my case, since I only write in one
thread, it is safe.
Not if you are reading from other threads. You really need a multiple
reader/single writer lock to synchronize access to a DataSet.
Post by William Sullivan
Post by Miha Markic [MVP C#]
but what about DataViews from the DefaultView
Post by William Sullivan
accessor? Here's the situation: ASP.NET application; DataSets in the Cache;
Each session gets a DataView from each DataSet via DefaultView, applies a
filter to that DataView, and displays the result to the user. From what I
understand, if two threads grab the same DefaultView from the same DataSet,
and each applies a filter to it, the data displayed to both users will
be
the
same (it will be what is allowed by the last filter applied). Is this true?
No.
So, DefaultView doesn't return the same object every time it's called? If
it does, then if two threads give it different filters at the same time, the
last filter applied would be shown to both users. If it returns a different
object each time its called, then this would obviously be the case. So which
is it; same object, or different object?
It returns the same object.
William Sullivan
2005-08-29 16:56:03 UTC
Permalink
Post by Nigel Norris
Not if you are reading from other threads. You really need a multiple
reader/single writer lock to synchronize access to a DataSet.
Really? That goes against what it says in the documentation. "This type is
safe for multithreaded read operations. You must synchronize any write
operations." Why would I need to synch?
Post by Nigel Norris
Post by William Sullivan
So, DefaultView doesn't return the same object every time it's called? If
it does, then if two threads give it different filters at the same time, the
last filter applied would be shown to both users. If it returns a different
object each time its called, then this would obviously be the case. So which
is it; same object, or different object?
It returns the same object.
So, then what I said is true? Two different threads acting on the same
object (retrieved from DataSet.DefaultView) may end up stepping on each other
if they try to set the Filter value?
Nigel Norris
2005-08-29 20:39:40 UTC
Permalink
Post by William Sullivan
Post by Nigel Norris
Not if you are reading from other threads. You really need a multiple
reader/single writer lock to synchronize access to a DataSet.
Really? That goes against what it says in the documentation. "This type is
safe for multithreaded read operations. You must synchronize any write
operations." Why would I need to synch?
You could interpret the documentation the way you have. However I don't
think that's the intent - though it's not clear enough without further
qualification.

It says "This type is safe for multithreaded read operations." It doesn't
say "This type is safe for multithreaded read operations and one active
write".

The secondary statement leaves unsaid what it means by 'synchronize' for the
write operations - my interpretation is exclusive access - that means no
other readers, as well as writers.

If you think about it, this makes sense. A DataSet is a very complex data
structure. It would quite difficult to implement writes in a way that was
not thread safe, while preventing read operations from seeing intermediate
states of the data structure.

Further, I can't find any evidence of synchronization in the code.

As a final piece of evidence, the following reference reports a multiple
readers failure that turned out to be due to DataTable.Select being
considered as a write operation! This was from Microsoft Support, so they
obviously interpreted the documentation the way I do.

http://www.interact-sw.co.uk/iangblog/2004/04/26/rwlockvsmonitor

Given the lack of clarity about just what is a read operation and what
isn't, I would try to avoid building an application that shared a DatSet
between threads.
Post by William Sullivan
Post by Nigel Norris
Post by William Sullivan
So, DefaultView doesn't return the same object every time it's called?
If
it does, then if two threads give it different filters at the same
time,
the
last filter applied would be shown to both users. If it returns a different
object each time its called, then this would obviously be the case. So which
is it; same object, or different object?
It returns the same object.
So, then what I said is true? Two different threads acting on the same
object (retrieved from DataSet.DefaultView) may end up stepping on each other
if they try to set the Filter value?
Yes, they may step on each other in unpredictable ways (setting the filter
value is probably considered a write operation and hence requires exclusive
access).

Loading...