1

I'm using .net 3.5 datatable and add rows to a datatable from more than one thread. when not usin lock I get mismatch between the datatablr rows count and the maximum index. When trying to serialize the table i get an exception there is no rows at poitions X. of course I can lock the datatable but I would like to know what is the cause. I would expect the row.count() method to return base on the row maxmimum index +1 and not the have a non independed counter. also, where can I find the internal implementation of rows.Add (params) method? it seems like the counter goes up per adding even if in soem cases the adding is on the same row.

Edit: thanks for the comments. I do know it is not thread safe for write and already solve it by using lock on the syncroot object. I would like to see what happen internally in the add method to see where the count get messed up.

4
  • 1
    citation from DataTable docs : This type is safe for multithreaded read operations. You must synchronize any write operations. (msdn.microsoft.com/en-us/library/…) Commented Sep 21, 2012 at 13:09
  • Post your example code so we can try and replicate the problem. Commented Sep 21, 2012 at 13:09
  • you could easy reproduce it: DataTable dt = new DataTable(); for (int i = 0; i < 500; i++) { new Thread(()=> dt.Rows.Add(new object[] {})).Start(); } now check the count value and the list of row. they are not equals. Commented Sep 21, 2012 at 13:28
  • 1
    @AntonioBakula, if I could +1 that comment a million times I would. One very common mistake in programming is assuming that multi-threading is just fully encapsulated and you don't have to worry about it. It's almost as big a mistake as people thinking because there is "garbage collection" in .NET they don't have to concern themselves with how they setup and scope their object references. Commented Sep 21, 2012 at 13:40

1 Answer 1

4

The cause is simple: DataTable is not thread-safe. It makes no claim to be thread-safe (against competing writes), and it is not required to be thread-safe. Most code is not thread-safe, and unless something explicitly says "I'm thread-safe for the following scenarios", you should assume it isn't.

Most commonly, in this scenario, it means one of:

  • an index update is getting lost, i.e. both are doing count++ at the same time, at it goes up by only 1
  • an operation results in crazy data when the array is being copied, grown, shrunk or rearranged

The fix is simple: if any thread is changing the table, it must have exclusive access. So synchronize. lock is the easiest option, but more exotic options like ReaderWriterLockSlim exist.

Then the next step would be to decide: do we really want to be using DataTable?

Sign up to request clarification or add additional context in comments.

1 Comment

so the ReaderWriterLockSlim class uses the basic lock but just has more elaborate surroundings that are encapsulated away from you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.