Minimize lock time

Kagamin spam at here.lot
Thu Jun 10 00:39:11 PDT 2010


Justin Spahr-Summers Wrote:

> 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);

1. opSuccess must be called in lock.
2. this solution doesn't scale: there can be three or more such transitions.


More information about the Digitalmars-d-learn mailing list