What's the best pattern for making datasets threadsafe on write?
The best I can find by googling is 'implement a wrapper layer with locks' but at first blush this seems rather messy.
Can someone recommend / point me in the direction of a good solution to this? It seems likely to be a problem that has already been solved somewhere.
edit: I also need to bind the dataset to a ui grid, which complicates matters somewhat.
Answers. DataTable is not designed to be thread-safe for modifications for performance reasons (Write Operations). If we want to access and modify a DataTable object, we need to use the lock statement to synchronize.
write() is certainly thread-safe. The problem is that a partial write() could require multiple calls in order to completely write the data, and while that is "thread-safe" it could result in interleaved data.
++ is not defined as thread-safe.
A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.
"Implement a wrapper layer with locks" is the way to go.
The wrapper layer will likely use locking that is specific to the way your application uses the DataSets.
Attempting to design a generic solution for a class as complex as a DataSet is probably doomed to failure.
For example, enumerating properties will generally not be thread-safe - so you would need to hold a lock for as long as any caller is enumerating any of the many collection properties (DataSet.Tables, DataTable.Rows, ...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With