Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I have already asked a question question about a dynamic multi threaded lock, and the reviewer found a problem.

I have already asked a question about a dynamic multi threaded lock, and the reviewer found a problem.

I have already asked a question about a dynamic multi threaded lock, and the reviewer found a problem.

Rollback to Revision 3
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 210
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
 private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
 private static object _lock = new object();
 public static void ExecuteLock(string key, Action action)
 {
 try
 {
 bool isFree = true;
 do
 {
 lock (_lock)
 {
 if (!_locksByKeys.ContainsKey(key))
 _locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
 isFree = Monitor.TryEnter(_locksByKeys[key]);
 }
 if (!isFree)
 {
 Thread.Sleep(50);
 }
 } while (!isFree);
 action.Invoke();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 Monitor.Exit(_locksByKeys[key]);
 }
 }
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
 private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
 private static object _lock = new object();
 public static void ExecuteLock(string key, Action action)
 {
 try
 {
 bool isFree = true;
 do
 {
 lock (_lock)
 {
 if (!_locksByKeys.ContainsKey(key))
 _locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
 isFree = Monitor.TryEnter(_locksByKeys[key]);
 }
 if (!isFree)
 {
 Thread.Sleep(50);
 }
 } while (!isFree);
 action.Invoke();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 Monitor.Exit(_locksByKeys[key]);
 }
 }
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
 private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
 private static object _lock = new object();
 public static void ExecuteLock(string key, Action action)
 {
 try
 {
 bool isFree = true;
 do
 {
 lock (_lock)
 {
 if (!_locksByKeys.ContainsKey(key))
 _locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
 isFree = Monitor.TryEnter(_locksByKeys[key]);
 }
 if (!isFree)
 {
 Thread.Sleep(50);
 }
 } while (!isFree);
 action.Invoke();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 Monitor.Exit(_locksByKeys[key]);
 }
 }
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
Improved grammar/readability of intro
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
 private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
 private static object _lock = new object();
 public static void ExecuteLock(string key, Action action)
 {
 try
 {
 bool isFree = true;
 do
 {
 lock (_lock)
 {
 if (!_locksByKeys.ContainsKey(key))
 _locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
 isFree = Monitor.TryEnter(_locksByKeys[key]);
 }
 if (!isFree)
 {
 Thread.Sleep(50);
 }
 } while (!isFree);
 action.Invoke();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 Monitor.Exit(_locksByKeys[key]);
 }
 }
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
 private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
 private static object _lock = new object();
 public static void ExecuteLock(string key, Action action)
 {
 try
 {
 bool isFree = true;
 do
 {
 lock (_lock)
 {
 if (!_locksByKeys.ContainsKey(key))
 _locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
 isFree = Monitor.TryEnter(_locksByKeys[key]);
 }
 if (!isFree)
 {
 Thread.Sleep(50);
 }
 } while (!isFree);
 action.Invoke();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 Monitor.Exit(_locksByKeys[key]);
 }
 }
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
 private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
 private static object _lock = new object();
 public static void ExecuteLock(string key, Action action)
 {
 try
 {
 bool isFree = true;
 do
 {
 lock (_lock)
 {
 if (!_locksByKeys.ContainsKey(key))
 _locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
 isFree = Monitor.TryEnter(_locksByKeys[key]);
 }
 if (!isFree)
 {
 Thread.Sleep(50);
 }
 } while (!isFree);
 action.Invoke();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 Monitor.Exit(_locksByKeys[key]);
 }
 }
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
Improved grammar/readability of intro, removed extra(8) whitespaces from code formatting
Source Link
Loading
rm tag from title
Link
svick
  • 24.5k
  • 4
  • 53
  • 89
Loading
Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /