I’m helping spruce up an application, and it’s full of concurrency issues. I’m attempting to work through them, and came across a section that’s using DataTables.
The datatable itself is static, shared across many threads.
I know that using dt.Select(“…”) itself needs a lock statement, or you’ll have issues when adding/removing rows to the data table. However, when that call returns, you have an array of DataRow objects.
I’d obviously lock those rows if I was to update them, but if I’m just reading them, do they need locking?
Basically, given that elsewhere we’re adding new rows and potentially updating existing rows, which of these is correct:
lock (dtLock)
{
DataRow[] rows = dt.Select("...");
}
foreach(DataRow dr in rows)
{
// read statements only
}
or
lock (dtLock)
{
DataRow[] rows = dt.Select("...");
foreach(DataRow dr in rows)
{
// read statements only
}
}
Since you stated that you will be updating existing rows then you have no other choice but to lock access to the rows you extract from
Select. You cannot (or at least should not) access the rows, even just reads, if there is a possibility that they can be modified by another thread. Furthermore it is possible (and I have seen ancedotal evidence myself) that accessing an individual row can touch the underlyingDataTable‘s internal structure so even if you were just adding new rows there still might be a problem.