Re: DataTable weirdness

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Stephan Steiner (steiner_at_isuisse.com)
Date: 03/01/05


Date: Tue, 1 Mar 2005 14:46:30 +0100

Chris

Thanks for the link to the article (I've seen it on other occasions but
never read the entire thing).

I'm currently implementing locks into my software, but I'm wondering:

If I iterate through all the elements of a table in thread A, and at the
same time I write to a cell in the database from a thread B (the entire
write operation is encapsulated in a lock statement). The doc says DB reads
are thread safe. So, am I correct assuming that the lock in thread B will
block until the iterator is no longer used?

Come to think of it I've experienced similar conditions previously: while
iterating through the rows in a table, I find that I need to delete a row. I
can delete the row just fine inside the iteration but when I try to get the
next element via the foreach iterator, I get an exception because the
collection of lines has changed. So I ended up storing which lines I have to
delete, then after going through all lines, I deleted the lines marked for
deletion. In this case, what would an attempt to delete a record directly
within the iteration (using the lock statement) yield? Will the code in the
lock statement just be skipped because I'm currently interating through the
table rows?

Regards
Stephan

>
> If the method updating the data structure is an instance method, then
> locking "this" is what is usually done:
>
> lock(this)
> {
> ...
> myRow["timestamp"] = timestamp;
> ...
> }
>
> If the method is a static method, there will be no "this" to lock.
> In that case, the class needs to create an object to lock on:
>
> public class MyClass
> {
> private static readonly object countLock = new object();
>
> public static void UpdateMyRow()
> {
> lock(countLock)
> {
> ...
> myRow["timestamp"] = timestamp;
> ...
> }
> }
> }
>
>
> MVP Jon Skeet has written an excellent article about threading in C#:
>
> http://www.yoda.arachsys.com/csharp/threads/
>
> --
> Hope this helps.
>
> Chris.
> -------------
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/



Relevant Pages

  • Re: [PATCH 0/7] convert semaphore to mutex in struct class
    ... They should use a local lock instead. ... By far most of the usages of class.semaphore or class.mutex in drivers ... variation of this would be David's class_for_each_instanceiterator ...
    (Linux-Kernel)
  • Re: [PATCH 0/7] convert semaphore to mutex in struct class
    ... They should use a local lock instead. ... By far most of the usages of class.semaphore or class.mutex in drivers ... variation of this would be David's class_for_each_instanceiterator ...
    (Linux-Kernel)
  • RE: C# synchronized methods?
    ... > Just wrap the entire content of your method in a lock statement. ... > public class MyClass ... > public void MyMethod() ... I'll use the lock code, because that looks better IMO and I'll know what ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delete a list item from a separate thread safe?
    ... Will the iterator in thread 1 still step correctly if the item it's stepping ... One way could be to lock the whole iteration process and not only the increment operation. ... Or you need to trigger the clean-up operation in the same iterating task after it finished the list. ... Either you need a list type that supports concurrent operations (but you still need to lock the current "active" list item), or you need better list access algorighms. ...
    (microsoft.public.vc.stl)
  • Re: Threading newbie: Lock statement
    ... having a value type as the lock expression will cause a compilation error. ... Can I use the lock statement to synchronise ... public static void MySecondThreadProcedure() ... An integer is not a reference type, so you will be locking on a boxed ...
    (microsoft.public.dotnet.languages.csharp)