Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Parallel.forEach(DataTable.AsEnumerable() thread safe

when used as follows

Parallel.ForEach(DataTable.AsEnumerable(), dr => {

    string str = dr["field1"].ToString();
     //.... other stuff
    dr["f1"] = o.A;
    dr["f2"] = o.B;
    dr["f3"] = o.C;

});

where each thread works on its own datarow

I would assume not but but there's this saying about assumptions....

like image 832
Kumar Avatar asked Mar 28 '26 16:03

Kumar


1 Answers

The documentation of the DataRow class definitively states that

This type is safe for multithreaded read operations. You must synchronize any write operations.

Can't get any more specific than that.

In any case, parallel writing to a Datatable is probably not going to scale well. Scalability suffers when you have multiple threads accessing shared state and a single datatable is quite obviously shared state. What's more, unless you work with NUMA hardware your CPU cores will contend for access to the same memory bus.

A much better solution is to return any results from the parallel processing (the "other stuff" ) in a separate structure (e.g. one of the concurrent collections) and apply the changes from a single thread when the loop finishes.

Another option is to use PLINQ to calculate the results and and iterate over them with a simple foreach to apply the changes back to the DataTable.

An even better solution would be to discard the original datatable entirely and return a new object that contains the fields you require. Unless your code requires the result to be a DataTable, you could simply return the results as an IEnumerable

like image 100
Panagiotis Kanavos Avatar answered Apr 02 '26 22:04

Panagiotis Kanavos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!