Does c# have its own version of the java "synchronized" keyword?
I.e. in java it can be specified either to a function, an object or a block of code, like so:
public synchronized void doImportantStuff() {
// dangerous code goes here.
}
or
public void doImportantStuff() {
// trivial stuff
synchronized {
// dangerous code goes here.
}
}
-
Does c# have its own version of the java "synchronized" keyword?
No. In C#, you explicitly
lockresources that you want to work on synchronously across asynchronous threads.lockopens a block; it doesn't work on method level.However, the underlying mechanism is similar since
lockworks by invokingMonitor.Enter(and subsequentlyMonitor.Exit) on the runtime. Java works the same way, according to the Sun documentation. -
static object Lock = new object(); lock (Lock) { // do stuff }serg10 : Are you sure you want to declare your lock object as static..?Jan Gressmann : Sure, so every Thread can easily access it without passing references around. -
You can use the
lockstatement instead. I think this can only replace the second version. Also, remember that bothsynchronizedandlockneed to operate on an object. -
First - most classes will never need to be thread-safe. Use YAGNI: only apply thread-safety when you know you actually are going to use it (and test it).
For the method-level stuff, there is
[MethodImpl]:[MethodImpl(MethodImplOptions.Synchronized)] public void SomeMethod() {/* code */}This can also be used on accessors (properties and events):
private int i; public int SomeProperty { [MethodImpl(MethodImplOptions.Synchronized)] get { return i; } [MethodImpl(MethodImplOptions.Synchronized)] set { i = value; } }Note that field-like events are synchronized by default, while auto-implemented properties are not:
public int SomeProperty {get;set;} // not synchronized public event EventHandler SomeEvent; // synchronizedPersonally, I don't like the implementation of
MethodImpl(or field-like events) as it locksthisortypeof(Foo)- which is against best practice. The preferred option is to use your own locks:private readonly object syncLock = new object(); public void SomeMethod() { lock(syncLock) { /* code */ } }This allows more granular usage, and allows use of
Monitor.Wait/Monitor.Pulseetc to communicate between threads.A related blog entry.
0 comments:
Post a Comment