digitalmars.D.learn - Minimize lock time
- Kagamin (14/14) Jun 09 2010 Let's consider the following code:
- Justin Spahr-Summers (9/28) Jun 10 2010 A common pattern is to use a boolean flag:
- Kagamin (3/34) Jun 10 2010 1. opSuccess must be called in lock.
- Simen kjaeraas (43/58) Jun 10 2010 A flag, as has been mentioned, would work.
- Justin Spahr-Summers (6/56) Jun 10 2010 All good suggestions. synchronized(), in general, just doesn't scale
- Steven Schveighoffer (18/33) Jun 14 2010 What about using a Mutex object?
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
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
Justin Spahr-Summers Wrote:On Thu, 10 Jun 2010 02:54:37 -0400, Kagamin <spam here.lot> wrote:1. opSuccess must be called in lock. 2. this solution doesn't scale: there can be three or more such transitions.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
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
On Thu, 10 Jun 2010 12:42:09 +0200, Simen kjaeraas <simen.kjaras gmail.com> wrote:Kagamin <spam here.lot> wrote: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.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);
Jun 10 2010
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