Adam and Greg are suggesting not using lock(this) because "this" is
technically a public member, meaning anything in the system could lock on
that as well and cause a deadlock in your code that you will be unable to
resolve without switching to a private member.

On Fri, 11 Aug 2006 10:31:55 -0400, Eddie Lascu <[EMAIL PROTECTED]>
wrote:

>Hello Adam & Gregory,
>
>I see you both suggesting that I should lock on a private lock object
>instead of "this". Could you please explain why? I know locking on value
>members doesn't work, because of the boxing that takes place. So if you
only
>have value members you need to "invent" a private object (perhaps a
string)
>and use it as a lock. Because of that, using "this" seemed like a natural
>choice.
>
>I agree that with so much locking, performance can be hurt, but in my
case I
>don't think this can be an issue. We are talking about a few financial
>transactions a minute.
>
>Thanks guys for your time.
>Regards,
>Eddie
>
>
>-----Original Message-----
>From: Discussion of advanced .NET topics.
>[mailto:[EMAIL PROTECTED] Behalf Of Adam Sills
>Sent: Thursday, August 10, 2006 6:56 PM
>To: [email protected]
>Subject: Re: [ADVANCED-DOTNET] Multithread-safe classes
>
>
>This is a much better example. You shouldn't lock this and should lock a
>private lock object instead. You shouldn't use volatile since you have
more
>than one variable that may change at a time. The properties should also
lock
>the same lock object because you don't want a property to execute in the
>middle of your locked AddVistaTx method and get an invalid set of data.
>
>The lock will definitely hurt performance, but you really don't have a
>choice. And worrying about lack of inlining (or other performance
>characteristics in general) is mostly useless until you can actually have
>something to performance test. Worrying about one minor CIL instruction
(and
>yes, if lock injects a finally it will execute even with a return in
there)
>should be the least of your concerns.
>
>Adam..
>
>> -----Original Message-----
>> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
>> [EMAIL PROTECTED] On Behalf Of Eddie Lascu
>> Sent: Thursday, August 10, 2006 5:27 PM
>> To: [email protected]
>> Subject: Re: [ADVANCED-DOTNET] Multithread-safe classes
>>
>> Hi Gregory,
>>
>> I tried to simplify the class and by doing that, my problem mandates a
>> simpler solution. Here is a more accurate version of the class:
>>
>> public sealed class CCTxBatch
>> {
>>         public CCTxBatch(string strTerminalId)
>>         {
>>         strTerminalId_ = strTerminalId;
>>         }
>>
>>         public int VisaTransactionsCount
>>         {
>>                 get
>>                 {
>>                         return nVisaTxCount_;
>>                 }
>>         }
>>         public double VisaTransactionsAmount
>>         {
>>                 get
>>                 {
>>                         return dVisaTxTotalAmount_;
>>                 }
>>         }
>>         // next ones here
>>
>>         public void ResetCountsAndTotals()
>>         {
>>                 nVisaTxCount_ = 0;
>>                 dVisaTxTotalAmount_ = 0.0;
>>
>>                 nAmexTxCount_ = 0;
>>                 dAmexTxTotalAmount_ = 0.0;
>>
>>                 nMCardTxCount_ = 0;
>>                 dMCardTxTotalAmount_ = 0.0;
>>         }
>>
>>         public void     AddVisaTx( double dAmount )
>>         (
>>                 nVisaTxCount_++;
>>                 dVisaTxTotalAmount_ += dAmount;
>>         }
>>
>>         public void AddAmexTx( double dAmount )
>>         (
>>                 nAmexTxCount_++;
>>                 dAmexTxTotalAmount_ += dAmount;
>>         }
>>
>>         public void AddMCardTx( double dAmount )
>>         (
>>                 nMCardTxCount_++;
>>                 dMCardTxTotalAmount_ += dAmount;
>>         }
>>
>>       int nVisaTxCount_;
>>       double dVisaTxTotalAmount_;
>>
>>       int nAmexTxCount_;
>>       double dAmexTxTotalAmount_;
>>
>>       int nMCardTxCount_;
>>       double dMCardTxTotalAmount_;
>>
>>         string strTerminalId_;
>> }
>>
>> I want to have a single lock for all members, because there are yet
other
>> methods that can change multiple pairs. The methods do not return
>> anything,
>> so I can have all the code inside the method wrapped into a lock(this).
I
>> was curios about the properties.
>>
>> Regards,
>> Eddie
>>
>>
>
>===================================
>This list is hosted by DevelopMentor®  http://www.develop.com
>
>View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to