Re: Thread Locking In Static Methods - How?

From: John M Deal (johndeal_at_necessitysoftware.com)
Date: 11/15/04


Date: Mon, 15 Nov 2004 09:42:56 -0800

Great catch. The funny thing is that I've actually implemented it in
code the way you show it and not the way I did. Some time ago I had read
Jeff Richter's article
(http://msdn.microsoft.com/msdnmag/issues/03/01/NET/) on this topic and
I use it as a reference anytime I need it.

I suppose where I screwed up was by responding from memory rather than
being sure to check the real thing. Thanks for calling me on it though,
it'll help remind me to check my references (and code) before passing on
  what could turn out to be detrimental information to others.

Have A Better One!

John M Deal, MCP
Necessity Software

Richard Blewett [DevelopMentor] wrote:
> Yes, the lock keyword is a terribly named keyword
>
> http://www.dotnetconsult.co.uk/weblog/PermaLink.aspx/870417fa-dc59-4c8d-9dad-a68b98b24457
>
> However, I'm not sure about your implementation. Three things:
>
> 1. Its a bit heavyweight to allocate a string just for locking
> 2. Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned
> 3. If anyone updates the string they will end up with a different object and so you then have a race condition
>
> Its much simpler to use
>
> private static readonly object myGuardObject = new object();
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>
> No. What this does is cause a lock of any code location that uses the
> same lock definition. If every method in the class had its code
> encompassed by the same statement it would cause the whole class to lock
> anytime one of the methods was entered, but only if that was the case.
>
> If you want to make sure that only the one method is locked you could
> create a private static member variable for the specific method, then
> lock against that. For example:
>
> private static string _logTextLock = "This is used to lock.";
> public static void LogText(string text)
> {
> lock (_logTextLock)
> {
> ...
> }
> }
>
> Hope that helps.
>
> Have A Better One!
>
> John M Deal, MCP
> Necessity Software
>
>
>



Relevant Pages

  • Re: [PATCH] tracing/lockdep: turn lock->name into an array
    ... the "lock acquired" event is traced using a TRACE_EVENT. ... But we can't use the char * type for the name without risking to ... I guess it only happend at module unloading. ... make delayed string table freeing at module unloading. ...
    (Linux-Kernel)
  • Re: Thread Locking In Static Methods - How?
    ... the lock keyword is a terribly named keyword ... Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned ... What this does is cause a lock of any code location that uses the ...
    (microsoft.public.dotnet.languages.csharp)
  • IM Lock 2006 - Insecure Registry Permission Vulnerability
    ... IM Lock 2006 discloses passwords to local users. ... (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) ... Dim GetCrypt, Decrypt As String ... Dim lResult As Long ...
    (Bugtraq)
  • Re: Floyd Rose tuning stability
    ... No string lubricants or anything like that. ... If you're able to test the pitch of the string section between the locking ... screws coming out the back that lock the strings in, ... Strat-style whammy bar. ...
    (alt.guitar)
  • Re: using clustered index to optimize inserts ...
    ... I will try to explain locking in terms of Sybase docs... ... Allpages Locking: Allpages locking locks both data pages and index ... the data page is locked with an exclusive lock. ... Clustered Index: The datarows will be arranged as per the clustered ...
    (comp.databases.sybase)