I have a measuring instrument object:
public class Instrument
{
public double Measure()
{
return 0;
}
}
I have a device that needs to do some measuring:
public class Device
{
public Instrument MeasuringInstrument { get; set; }
public void DoMeasuring()
{
var result = this.MeasuringInstrument.Measure();
}
}
The measuring instrument can only operate on one device at a time, yet many devices may use the same instrument. I'm new to threading, and from what I understand, both of the following solutions have caveats.
public class Instrument
{
public double Measure()
{
lock(this)
{
return 0;
}
}
}
public class Device
{
public Instrument MeasuringInstrument { get; set; }
public void DoMeasuring()
{
lock(this.MeasurementInstrument)
{
var result = this.MeasuringInstrument.Measure();
}
}
}
I've read it's best to lock on private objects, but I don't know how to do that while still allowing the MeasuringInstrument to be get/set on the Device. Any suggestions?
Thanks much,
Ken
The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.
Integers can not be used with lock because they are boxed (and lock only locks on references). The scenario is as follows: I have a forum based website with a moderation feature. What I want to do is make sure that no more than one moderator can moderate a post at any given time.
The lock statement is one of the simplest and most common tools for C# developers writing multithreaded applications. It can be used to synchronize access to blocks of code, achieving thread safety by allowing only one thread at a time to execute the code in that block.
A lock may be a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: just one thread at a time can acquire the lock and everyone accesses to the shared resource requires that the lock be acquired first.
if your instrument
is used by multiple devices
the best practice is to set lock
in your instrument
class. so the fist solution works better.
but its better to create a new lock object and use it in instrument
class.
public class Instrument
{
Object lockKey = new Object();
public double Measure()
{
lock(lockKey)
{
return 0;
}
}
}
the usual pattern is to create your own private object
just for locking in the case where the obvious choice might be exposed outside of the class, for example:
public class Instrument
{
private object thisLock = new object();
public double Measure()
{
lock(this.thisLock)
{
return 0;
}
}
}
public class Device
{
public Instrument MeasuringInstrument { get; set; }
private object measuringInstrumentLock = new object();
public void DoMeasuring()
{
lock(this.measuringInstrumentLock)
{
var result = this.MeasuringInstrument.Measure();
}
}
}
Also, I suspect that you only need one of those two locks (either the one in DoMeasuring
or the one in Measure
) although that does depend on the missing bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With