Minimize lock time

Justin Spahr-Summers Justin.SpahrSummers at gmail.com
Thu Jun 10 00:34:24 PDT 2010


On Thu, 10 Jun 2010 02:54:37 -0400, 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 common pattern is to use a boolean flag:

bool success;
synchronized (syncRoot)
{
  success = (condition);
}

if (success) opSuccess();
else writeln(possibly, slow);


More information about the Digitalmars-d-learn mailing list