Minimize lock time
    Simen kjaeraas 
    simen.kjaras at gmail.com
       
    Thu Jun 10 03:42:09 PDT 2010
    
    
  
Kagamin <spam at here.lot> wrote:
> Let's consider the following code:
>
> synchronized(syncRoot)
> {
>   if(condition)opSuccess();
>   else writeln(possibly,slow);
> }
>
> Suppose the else close doesn't need to be executed in lock domain and  
> can be slow. How to minimize lock time here?
>
> synchronized(syncRoot)
> {
>   if(condition)opSuccess();
>   else goto Lwrite;
> }
> Lwrite: writeln(possibly,slow);
>
> We can do this... but...
A flag, as has been mentioned, would work.
Other solutions that might work:
////////
synchronized(syncRoot)
{
   if (condition)
   {
     opSuccess();
     return;
   }
}
writeln(possibly,slow);
////////
do
{
   synchronized(syncRoot)
   {
     if (condition)
     {
       opSuccess();
       break;
     }
   }
   writeln(possibly, slow);
} while (false);
////////
bool helperFunc( ) {
   synchronized( syncRoot )
   {
     if ( condition ) {
       opSuccess();
       return true;
     } else {
       return false;
     }
   }
}
if (!helperFunc( )) {
    writeln( possibly, slow );
}
--
Simen
    
    
More information about the Digitalmars-d-learn
mailing list