Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lock
Message
 
À
20/05/2011 10:37:33
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Re: Lock
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Divers
Thread ID:
01511264
Message ID:
01511283
Vues:
45
>Hi all,
>
>I have this method I am refactoring for various reasons. There is a lot of nesting and plan to remove some of it. I am wondering about this intital test inside of a lock statement and wonder if it would be safe to move it outside of the lock construct.

>Wondering if it would be just as safe to do this and remove the extra if structure.
>
>public void FindActiveWebService()
>{
>    // Moved outside the lock
>    if (IsFindingActiveWebService)
>        return;
>
>    lock (FindWebServiceSync)
>    {
>        IsFindingActiveWebService = true;
>        try
>        {
>            // Lots of crappy code and nested code in here with loops and such.
>            // Planning to refactor all this junk.
>        }
>        finally
>        {
>            IsFindingActiveWebService = false;
>        }
>    }
>}
>
Let's supposed IsFindingActiveWebService is false. One thread calls FindActiveWebService() and makes it past the return. At the same time, another thread calls this same code. It also makes it past this check. The first thread has locked FindWebServiceSync, so the other thread is currently blocked. It appears like the only reason for IsFinidingActiveWebService (if it's not used anywhere else) is to keep threads from piling up here under heavy loads (although you could do this with just the FindWebServicesSync by checking if it's locked).

( Ah, I just read you're reply & Vivs), yep, looks redundant.
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform