views:

154

answers:

1

I have two threads, a producer thread that places objects into a generic List collection and a consumer thread that pulls those objects out of the same generic List. I've got the reads and writes to the collection properly synchronized using the lock keyword, and everything is working fine.

What I want to know is if it is ok to access the Count property without first locking the collection.

JaredPar refers to the Count property in his blog as a decision procedure that can lead to race conditions, like this:

if (list.Count > 0)
{
    return list[0];
}

If the list has one item and that item is removed after the Count property is accessed but before the indexer, an exception will occur. I get that.

But would it be ok to use the Count property to, say, determine the initial size a completely different collection? The MSDN documentation says that instance members are not guaranteed to be thread safe, so should I just lock the collection before accessing the Count property?

+6  A: 

I suspect it's "safe" in terms of "it's not going to cause anything to go catastrophically wrong" - but that you may get stale data. That's because I suspect it's just held in a simple variable, and that that's likely to be the case in the future. That's not the same as a guarantee though.

Personally I'd keep it simple: if you're accessing shared mutable data, only do so in a lock (using the same lock for the same data). Lock-free programming is all very well if you've got appropriate isolation in place (so you know you've got appropriate memory barriers, and you know that you'll never be modifying it in one thread while you're reading from it in another) but it sounds like that isn't the case here.

The good news is that acquiring an uncontested lock is incredibly cheap - so I'd go for the safe route if I were you. Threading is hard enough without introducing race conditions which are likely to give no significant performance benefit but at the cost of rare and unreproducible bugs.

Jon Skeet
@Jon Skeet: Man, I feel blessed to have you weigh in on this! In this case, a stale value is perfectly ok because it is simply used to determine a 'rough' initial size of another collection. What I can't afford is the Count property returning a negative or really large value. Is it possible for this to happen if another thread is in the process of modifying the collection at the same time I pull the Count value? Count is an int, so even if it's modified, it'll be done atomically, right?
Matt Davis
@Matt: I'd *expect* this to be okay - but I'd still play it safe in my own code. You'd be relying on implementation details which could change in later versions. Yes, it's probably just stored in a simple `int` variable, but do you really want to rely on that? Are you concerned about performance, or code simplicity? If you're using C# 3 and locking on the collection itself, you could create an extension method ("LockedCount()" or something similar) to make it a bit easier.
Jon Skeet
@Jon Skeet: You're right. Without a guarantee of its current and future implementation, it would be unwise to guess. Thanks so much!
Matt Davis