[dba-VB] c# lock()

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Sat Mar 26 19:38:28 CDT 2011


Hi John --

<<<
If locking the object itself works, why do the static thing?
>>> 
Static is used to lock shared resources - in your case there are no such
resources at all?
Log writer is usually a static shared resource...

And yes, you can use your locking code to lock parallel access to your class
instances' local variables:

>>            private Object thisLock = new Object();
>>
>>            public DateTime pFlagDte {
>>                get {
>>                    lock (thisLock)
>>                    {
>>                        return dteFlag;
>>                    }
>>                }
>>                set
>>                {
>>                    lock (thisLock)
>>                    {
>>                        dteFlag = value;
>>                    }
>>                }
>>            }

But as I noted I'm not sure if that "micro-level locking" is needed at all
(and you mentioned you have several such shared variables within a class
instance) - using Queues seems to be more simple and (thread-)safe approach
without almost any custom locking schemes....

Thank you.

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

I'm just trying to get a handle on locking things (in general).

I am seeing things that say "just lock the object itself", and then other
things that say "use a static object (the way you were showing me).

If locking the object itself works, why do the static thing?



John W. Colby
www.ColbyConsulting.com

On 3/26/2011 6:27 PM, Shamil Salakhetdinov wrote:
> John --
>
> Let's define your target (application) system architecture on 
> conceptual level - I guess that using "micro-level locking" you're 
> trying (?) to achieve isn't needed...
>
> 1) you have a huge input data set;
> 2) you split input data set into X chunks;
> 3) every chunk gets processed using the same several - Y - steps;
> 4) on completion of step Yn input/semi-processed chunk Xn is submitted 
> to the Y(n+1) processor class;
> 5) when all X chunks are processed in Y steps they get collected into 
> a final "huge" output set to produce Z reports.
>
> That's it from conceptual high level point of view?
> Something important missing?
> Too simplified to be true?
>
> John, please take into account that I can't reply promptly here - it's 
> weekend now and I'll be out tomorrow, and it's 1:26 a.m. - time to 
> sleep, and then I have quite some work to do starting Monday - so I 
> expect this thread will be supported by other AccessD/dba-VB members...
>
> Thank you.
>
> --
> Shamil
>
> -----Original Message-----
> From: dba-vb-bounces at databaseadvisors.com
> [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
> Sent: 27 ????? 2011 ?. 0:53
> To: Discussion concerning Visual Basic and related programming issues.
> Subject: Re: [dba-VB] c# lock()
>
> Shamil,
>
> I am trying to build a base class which contains a set of lock objects 
> and some standard variables.
>    Having defined the lock objects static per your code below, the 
> derived class cannot access the lock objects even though I set them
protected:
>
> protected static Object LockBln = new Object();
>
> In the derived class when I try to access it:
>
> lock(this.LockBln)
> {
> }
>
> I get a compile error:
>
> "Member cannot be accesses with an instance reference; Qualify it with 
> a type name instead."
>
> What does this mean?
>
> Reading on the internet it says that static properties are shared 
> between all instances of the class.  I don't see how I can share a 
> property between 50 instances and then use a
> lock(MyStaticProperty) {}.
>
> I'm so confused...
>
> 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





More information about the dba-VB mailing list