www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Minimize lock time

reply Kagamin <spam here.lot> writes:
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...
Jun 09 2010
next sibling parent reply Justin Spahr-Summers <Justin.SpahrSummers gmail.com> writes:
On Thu, 10 Jun 2010 02:54:37 -0400, Kagamin <spam 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);
Jun 10 2010
parent Kagamin <spam here.lot> writes:
Justin Spahr-Summers Wrote:

 On Thu, 10 Jun 2010 02:54:37 -0400, Kagamin <spam 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.
Jun 10 2010
prev sibling next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Kagamin <spam 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
Jun 10 2010
parent Justin Spahr-Summers <Justin.SpahrSummers gmail.com> writes:
On Thu, 10 Jun 2010 12:42:09 +0200, Simen kjaeraas 
<simen.kjaras gmail.com> wrote:
 
 Kagamin <spam 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.
Jun 10 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 10 Jun 2010 02:54:37 -0400, Kagamin <spam 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...
What about using a Mutex object? mut.lock(); if(condition) { scope(exit) mut.unlock(); opSuccess(); } else { mut.unlock(); writeln(possibly, slow); } I think it's in core.sync or something like that. Mutex can even take over the standard monitor element of another object, so for instance you could assign it as the monitor of your syncRoot, so your other code that uses syncRoot and synchronized still works. -Steve
Jun 14 2010