Is there any way to lock on an integer in C#? Integers can not be used with lock because they are boxed (and lock only locks on references).
The scenario is as follo
I've read a lot of comments mentioning that locking isn't safe for web applications, but, other than web farms, I haven't seen any explanations of why. I would be interested in hearing the arguments against it.
I have a similar need, though I'm caching re-sized images on the hard drive (which is obviously a local action so a web farm scenario isn't an issue).
Here is a redone version of what @Configurator posted. It includes a couple features that @Configurator didn't include:
Here's the code...
///
/// Provides a way to lock a resource based on a value (such as an ID or path).
///
public class Synchronizer
{
private Dictionary mLocks = new Dictionary();
private object mLock = new object();
///
/// Returns an object that can be used in a lock statement. Ex: lock(MySync.Lock(MyValue)) { ... }
///
///
///
public SyncLock Lock(T value)
{
lock (mLock)
{
SyncLock theLock;
if (mLocks.TryGetValue(value, out theLock))
return theLock;
theLock = new SyncLock(value, this);
mLocks.Add(value, theLock);
return theLock;
}
}
///
/// Unlocks the object. Called from Lock.Dispose.
///
///
public void Unlock(SyncLock theLock)
{
mLocks.Remove(theLock.Value);
}
///
/// Represents a lock for the Synchronizer class.
///
public class SyncLock
: IDisposable
{
///
/// This class should only be instantiated from the Synchronizer class.
///
///
///
internal SyncLock(T value, Synchronizer sync)
{
Value = value;
Sync = sync;
}
///
/// Makes sure the lock is removed.
///
public void Dispose()
{
Sync.Unlock(this);
}
///
/// Gets the value that this lock is based on.
///
public T Value { get; private set; }
///
/// Gets the synchronizer this lock was created from.
///
private Synchronizer Sync { get; set; }
}
}
Here's how you can use it...
public static readonly Synchronizer sPostSync = new Synchronizer();
....
using(var theLock = sPostSync.Lock(myID))
lock (theLock)
{
...
}