Skip to main content
rm tag from title
Link
svick
  • 24.3k
  • 4
  • 52
  • 89

Dynamic Multithreading lock - C#

edited grammar
Source Link
TheCoffeeCup
  • 9.4k
  • 4
  • 36
  • 96

I need to create an structure to control dynamically locks. For example, I have 100 threads running, and each thread will "process" a "key", sometimes. Sometimes two threads could be processing the same "key", and if it occurs, a dynamic lock needneeds to be created and used by both, to process one at a time.

I create the following structure and I would like a code review, to foundfind possible bugsbugs (even if it is extremely rare):

  // This is the class who control to create one different lock for each key.
    public class DynamicLockGenerator
{
    private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();

    private static object _lock = new object();

    public static objectGetLock(string key)
    {
        lock (_lock)
        {
            if (!_locksByKeys.ContainsKey(key))
                _locksByKeys.Add(key, new object());

            return _locksByKeys[key];
        }
    }

    public static void RemoveLock(string key)
    {
        lock (_lock)
        {
            //This line is extremely important: One lock object can only be removed
            //if no thread are using it.
            if (Monitor.TryEnter(_locksByKeys[key]))
            {
                _locksByKeys.Remove(key);
            }
        }
    }
}

//This is the use of the class. All threads do the same thing, but with different (or equal) keys.
object lockObj = DynamicLockGenerator.GetLock(key);

lock (lockObj)
{
    //PROCESS THE KEY
}

DynamicLockGenerator.RemoveLock(key);

I need to create an structure to control dynamically locks. For example, I have 100 threads running, and each thread will "process" a "key", sometimes two threads could be processing the same "key", and if it occurs, a dynamic lock need to be created and used by both, to process one at a time.

I create the following structure and I would like a code review, to found possible bugs (even if it is extremely rare):

  // This is the class who control to create one different lock for each key.
    public class DynamicLockGenerator
{
    private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();

    private static object _lock = new object();

    public static objectGetLock(string key)
    {
        lock (_lock)
        {
            if (!_locksByKeys.ContainsKey(key))
                _locksByKeys.Add(key, new object());

            return _locksByKeys[key];
        }
    }

    public static void RemoveLock(string key)
    {
        lock (_lock)
        {
            //This line is extremely important: One lock object can only be removed
            //if no thread are using it.
            if (Monitor.TryEnter(_locksByKeys[key]))
            {
                _locksByKeys.Remove(key);
            }
        }
    }
}

//This is the use of the class. All threads do the same thing, but with different (or equal) keys.
object lockObj = DynamicLockGenerator.GetLock(key);

lock (lockObj)
{
    //PROCESS THE KEY
}

DynamicLockGenerator.RemoveLock(key);

I need to create an structure to control dynamically locks. For example, I have 100 threads running, and each thread will "process" a "key". Sometimes two threads could be processing the same "key", and if it occurs, a dynamic lock needs to be created and used by both, to process one at a time.

I create the following structure and I would like a code review, to find possible bugs (even if it is extremely rare):

  // This is the class who control to create one different lock for each key.
    public class DynamicLockGenerator
{
    private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();

    private static object _lock = new object();

    public static objectGetLock(string key)
    {
        lock (_lock)
        {
            if (!_locksByKeys.ContainsKey(key))
                _locksByKeys.Add(key, new object());

            return _locksByKeys[key];
        }
    }

    public static void RemoveLock(string key)
    {
        lock (_lock)
        {
            //This line is extremely important: One lock object can only be removed
            //if no thread are using it.
            if (Monitor.TryEnter(_locksByKeys[key]))
            {
                _locksByKeys.Remove(key);
            }
        }
    }
}

//This is the use of the class. All threads do the same thing, but with different (or equal) keys.
object lockObj = DynamicLockGenerator.GetLock(key);

lock (lockObj)
{
    //PROCESS THE KEY
}

DynamicLockGenerator.RemoveLock(key);
Source Link

Dynamic Multithreading lock - C#

I need to create an structure to control dynamically locks. For example, I have 100 threads running, and each thread will "process" a "key", sometimes two threads could be processing the same "key", and if it occurs, a dynamic lock need to be created and used by both, to process one at a time.

I create the following structure and I would like a code review, to found possible bugs (even if it is extremely rare):

  // This is the class who control to create one different lock for each key.
    public class DynamicLockGenerator
{
    private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();

    private static object _lock = new object();

    public static objectGetLock(string key)
    {
        lock (_lock)
        {
            if (!_locksByKeys.ContainsKey(key))
                _locksByKeys.Add(key, new object());

            return _locksByKeys[key];
        }
    }

    public static void RemoveLock(string key)
    {
        lock (_lock)
        {
            //This line is extremely important: One lock object can only be removed
            //if no thread are using it.
            if (Monitor.TryEnter(_locksByKeys[key]))
            {
                _locksByKeys.Remove(key);
            }
        }
    }
}

//This is the use of the class. All threads do the same thing, but with different (or equal) keys.
object lockObj = DynamicLockGenerator.GetLock(key);

lock (lockObj)
{
    //PROCESS THE KEY
}

DynamicLockGenerator.RemoveLock(key);