Dataset thread: how thread safe?

G

Guest

Hi All
MSDN documentation explains the thread safety of the Dataset (and the underlying rows and tables) this way
"This type is safe for multithreaded read operations. You must synchronize any write operations.

Does this means that the following code is thread safe
public class Class

private Dataset1 m_ds = new Dataset1
public Class1(



public void AddEntry(string name, string description

lock(this

m_ds.LogEntries.AddLogEntriesRow( System.DateTime.Now, name, description )



public void GetEntry(int index, out string name, out string description

Dataset1.LogEntriesRow rw = m_ds.LogEntries[index]
description = rw.Description
name = rw.Description



My assumption is that datasets are not thread safe as soon as you start writting in it, so to be thread safe I would nee
public void GetEntry(int index, out string name, out string description

lock(this

Dataset1.LogEntriesRow rw = m_ds.LogEntries[index]
description = rw.Description
name = rw.Description



but maybe I misinterpret the documentation..

Thank
JPRoot
 
Y

Ying-Shen Yu[MSFT]

Hi,

Yes, your understanding is correct, DataSet is designed to be the
multi-reader or single writer. It's up to the consumer of the dataset to do
synchronization if they want to support multi-thread read/write.

Does it answer your question?


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Y

Ying-Shen Yu[MSFT]

Hi,

After a second look on your revised code, it does syhchronized the read and
write. however
it prevents the concurrent reading, there will always be only one read at
a time.
You may try using the ReaderWriteLock class to do locking.
For how to use the ReaderWriterLock you may refer to the documentation
below, there is also an example in it.
<ReaderWriterLock class>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemThreadingReaderWriterLockClassTopic.asp

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top