Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making datasets thread safe in C#

Tags:

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.

like image 399
burnside Avatar asked Feb 15 '10 18:02

burnside


People also ask

Is dataset thread-safe?

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.

Is C write thread-safe?

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 ++ thread-safe in C?

++ is not defined as thread-safe.

What is thread safety in C?

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.


1 Answers

"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, ...).

like image 195
Joe Avatar answered Oct 03 '22 18:10

Joe