Minimize lock time
Justin Spahr-Summers
Justin.SpahrSummers at gmail.com
Thu Jun 10 23:10:13 PDT 2010
On Thu, 10 Jun 2010 12:42:09 +0200, Simen kjaeraas
<simen.kjaras at gmail.com> wrote:
>
> 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);
All good suggestions. synchronized(), in general, just doesn't scale
very well (though no fault of D's). If you're doing very complex things,
you might have better luck with a semaphore of some kind. As always,
though, avoiding the need for synchronization is best.
More information about the Digitalmars-d-learn
mailing list