[dba-VB] c# lock()

jwcolby jwcolby at colbyconsulting.com
Thu Mar 10 16:27:16 CST 2011


Shamil,

Thanks for the code but I think it is overkill for my needs so far, though I will put it in the 
archives for when I need it.

In my system so far I have (or can arrange  to have ) only two threads trying to access a list.

Basically what I am doing is reading records out of sql server into a class factory, and placing the 
class instances into a list.

Another object with its own thread is coming by and looking at the objects in the list to see if 
there is any work for it to do.  If it finds an object to work on, it grabs a pointer to that item 
in the list and stores it in a pointer and releases the list.

One of the ways I saw this handled was to create factory which creates a brand new list object, 
grabs a pointer to the list, locks that and then copies the objects in the list pointers to classes) 
into that new list, unlocks the list and hands back a pointer to this brand new list.  thus the new 
list could be iterated at will.

I am just trying to lock the list while one thread adds objects to the list or another thread gets 
objects out of the list.

So if I have object A which contains a list and is adding / deleting objects to that list...
and object B which needs to iterate through the list looking for an object of interest in the list, 
and grabbing a pointer to that object, there is no way to do this without all of the code you wrote?

John W. Colby
www.ColbyConsulting.com

On 3/10/2011 4:35 PM, Shamil Salakhetdinov wrote:
>   Hi John --
>
> <<<
> Does that in fact lock the list to other objects doing the same thing
> (getting the list through that property)?
>>>>
> You have to lock/synchronize/serialize not the list itself but simultaneous
> access to the list from different threads.
> You'd better use special static object variable to implement locking.
>
> I will post sample code in my following posting here...
>
> Thank you.
>
> --
> Shamil



More information about the dba-VB mailing list