[dba-VB] c# lock()

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Fri Mar 25 17:46:44 CDT 2011


Hi John --

By resource I mean access to a read/write resource - it could be not only a
variable...

If you need two or more class level variables (fields, properties) within
one class instance to be locked then keep them in a class/structure and lock
that class/structure instance...

Thank you.

--
Shamil
 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: 26 ????? 2011 ?. 0:38
To: Discussion concerning Visual Basic and related programming issues.
Subject: Re: [dba-VB] c# lock()

When you say resource, you mean variable?

IOW use

private static Object LockBln = new Object(); private static Object LockDte
= new Object();

and use the LockBln to lock the boolean object while using LockDte to lock
the date object?

John W. Colby
www.ColbyConsulting.com

On 3/25/2011 5:05 PM, Shamil Salakhetdinov wrote:
> Hi John --
>
> Your sample is a correct usage of locking - just use static thisLock 
> variable
>
> private static Object thisLock = new Object()
>
> as non-static variable *is not* thread safe AFAIU.
>
> Yes, such locking can be done for various operations of that class - 
> just make sure you'll not get dead-locked...
> Also use *one lock object* to lock *one resource* in a class - 
> otherwise "deadlock" will become your "everyday guest"...
>
> Thank you.
>
> --
> Shamil
>
> -----Original Message-----
> From: dba-vb-bounces at databaseadvisors.com
> [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
> Sent: 25 ????? 2011 ?. 22:58
> To: Discussion concerning Visual Basic and related programming issues.
> Subject: Re: [dba-VB] c# lock()
>
> Shamil,
>
> Does this allow using a single "lock object" to lock various 
> operations in a class.
>
> For example I want to lock a date flag variable while using a property 
> to either get or set the variable.
>
>           private Object thisLock = new Object();
>
>           public DateTime pFlagDte {
>               get {
>                   lock (thisLock)
>                   {
>                       return dteFlag;
>                   }
>               }
>               set
>               {
>                   lock (thisLock)
>                   {
>                       dteFlag = value;
>                   }
>               }
>           }
>
> The issue is not multiple threads trying to write to the date variable 
> but rather one thread trying to read it while another is writing it.
>
> John W. Colby
> www.ColbyConsulting.com
>
> On 3/10/2011 5:51 PM, Shamil Salakhetdinov wrote:
>> Hi John --
>>
>> Do you you need add, remove and iterate a list to get an object or 
>> you need to add, remove and get an object by its key? - if the latter 
>> - here is the code you can use:
>>
>> private Dictionary<int, MyObject>   _myObjects = new Dictionary<int,
>> MyObject>();
>> private static object _myObjectsLocker = new object(); public void 
>> Add(MyObject myObject) {
>>       lock (_myObjectsLocker)
>>       {
>>           _myObjects.Add(myObject.ID, myObject);
>>           _keys.Add(myObject.ID);
>>       }
>> }
>> public MyObject Remove(int id)
>> {
>>       MyObject myobject = null;
>>       lock (_myObjectsLocker)
>>       {
>>           myobject = _myObjects[id];
>>           _myObjects.Remove(id);
>>       }
>>       return myobject;
>> }
>> public MyObject this[int key]
>> {
>>       get
>>       {
>>           MyObject myObject = null;
>>           lock (_myObjectsLocker)
>>           {
>>               _myObjects.TryGetValue(key, out myObject);
>>           }
>>           return myObject;
>>       }
>> }
>>
>> for the case of iteration just replace:
>>
>> _myObjects.TryGetValue(key, out myObject);
>>
>> with your iteration code.
>>
>> As for the sample code - changing qty of threads and changing total 
>> computer workload (outer context for the test code) you can see how 
>> different could be final results when heavy multi-threading is used.
>> And even if it's not heavy - it still can result in weird output if 
>> special multi-threading coding technique aren't used...
>>
>> Thank you.
>>
>> --
>> Shamil
>>
>> -----Original Message-----
>> From: dba-vb-bounces at databaseadvisors.com
>> [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
>> Sent: 11 ????? 2011 ?. 1:27
>> To: Discussion concerning Visual Basic and related programming issues.
>> Subject: Re: [dba-VB] c# lock()
>>
>> 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
>> _______________________________________________
>> dba-VB mailing list
>> dba-VB at databaseadvisors.com
>> http://databaseadvisors.com/mailman/listinfo/dba-vb
>> http://www.databaseadvisors.com
>>
>> _______________________________________________
>> dba-VB mailing list
>> dba-VB at databaseadvisors.com
>> http://databaseadvisors.com/mailman/listinfo/dba-vb
>> http://www.databaseadvisors.com
>>
>>
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
>
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
>
>
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list