Reputation: 180808
I have partial C# code for a blocking queue that looks like this:
private bool flushed;
private object _locker = new object();
public bool Flushed
{
get { lock (_locker) { return flushed; } }
set
{
lock (_locker)
{
flushed = value;
Monitor.Pulse(queue);
}
}
}
The Monitor.Pulse
method has a Monitor.Wait
counterpart in the Dequeue()
method of the blocking queue.
I want to add a method that signals end of data, which checks for the Flush
condition. It will look something like this:
public bool EndOfData
{
get { lock (_locker) { return Flushed && (queue.Count == 0); } }
}
Here is my question. Should I call the Flushed
property as shown in the code above (taking a nested lock
), or is it sufficient to refer to the private member variable flushed
directly, as shown below, using only a single lock?
public bool EndOfData
{
get { lock (_locker) { return flushed && (queue.Count == 0); } }
}
Upvotes: 3
Views: 153
Reputation: 33153
I don't think it makes a difference, personally I would use the nested version.
I think if anything changes in the actual property by using it, Flushed
, you ensure
that everything is good to go.
But I truly believe this is a preference call.
Upvotes: 2