-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write lock for properties #1670
Comments
Ooooh I googled around a bit but somehow didn't find that... |
I disagree. Anything of that nature is domain-specific and should therefor be done as explicitly as possible. The language as a whole shouldn't be in the business of promoting one specific synchronization mechanism, since that'll lead to people not thinking about which one is actually best suited to their domain (a mistake of the |
Also, sooner or later you will need to add another piece of code in that property outside the lock and you'd have to abandon this syntax altogether. |
This sounds very similar to |
Would be nice if the Instead of say... private readonly Mutex _mutex;
public Method() {
_mutex.WaitOne();
try {
// operation under lock
} finally {
_mutex.ReleaseMutex();
}
} One would be able to... private readonly Mutex _mutex;
public Method() {
lock(_mutex) {
// operation under lock
}
} ... it is just so much cleaner and easier to read. |
using System;
using System.Threading;
public static class MutexExtensions {
public static AcquiredMutex Lock(this Mutex mutex) {
mutex.WaitOne();
return new AcquiredMutex(mutex);
}
public class AcquiredMutex : IDisposable {
private readonly Mutex mutex;
public AcquiredMutex(Mutex mutex) => this.mutex = mutex;
void IDisposable.Dispose() => mutex.ReleaseMutex();
}
}
public class C {
public void M(Mutex mutex) {
using (mutex.Lock()) {
// operation under lock
}
}
} With #1623 we could avoid the allocation as well. |
@HaloFour I use that pattern all of the time, but it is still a work around for a "missing" language feature. |
@whoisj That code already compiles. Even though it does the wrong thing, I think it would be considered a breaking change to make it do the right thing. @HaloFour If you change |
Maybe. It works well enough for me and I have extension methods defined for most of the synchronization classes in the BCL or other major libraries. The issue with adding any kind of structural support to
Oh that's nice to know. I often use Rx's |
I know. Language flaw - doubt it can be overcome. 😞 Would have been nice if all of the language features relied on interfaces the way
Pipe dreams, I know. |
It now binds to |
If lock relied on a shape or interface then you wouldn’t be able to lock on any of the objects that you can today. |
WHAAAT?! 😁 😍 OMG OMG OMG... I need a cold shower, I'll be back in a bit. 😄 |
Now, if only that worked when the object is purely exposed as |
@crener - Your example isn't very representative though. You gain nothing by locking around a single field accessor like you do in that property. A lock is ideally used to avoid state being inconsistent while some algorithm runs, in that example you'd see no difference between using a lock or not using a lock. |
@whoisj - It seems that the C# lock was designed to help with locking within a single AppDomain which is why it's content to use (under the hood) a Win32 CriticalSection. Concurrent access across > 1 AppDomain is where a thing like a Mutex would come in. I guess concurrency across multiple AppDomains is something that has no inherent language support. |
@yaakov-h public static class LockingExtensions {
public static AcquiredLock Lock(this object lockobj) {
Monitor.Enter(lockobj);
return new AcquiredMutex(lockobj);
}
public readonly struct AcquiredLock : IDisposable {
private readonly object _lock;
public AcquiredLock(object _lock) => this._lock = _lock;
void IDisposable.Dispose() => Monitor.Exit(_lock);
}
} |
Wouldn't it be great if you had a lock which let multiple read actions into a critical section but blocked read actions once a write action is queued, this would help reduce the overhead of locks on commonly accessed resources by minimising the amount of waiting threads. In order to define these sections there needs to be a clear indication of intention as to whether a value is being written or read, such as proportions.
So by adding the lock before a normal property deceleration means that multiple threads can enter the getter but once a thread wants to enter the setter it blocks new threads entering either set or get until the setter thread leaves the setter method. If threads are already in the getter methods when a thread wants to set it waits until all threads leave.
The text was updated successfully, but these errors were encountered: