Reactions to story from Paul Stovell says…
Locking Rule #71
http://www.paulstovell.com/ blog/ locking-rule-71
Can you spot the danger in the following code? public T GetItem(Func<T, bool> finder) { T result = default(T); lock (_itemsLock) { foreach (T item in _items) { if (finder(item)) { result = item; break; } } } return result; } How about in this? public void HandleAdd(IEnumerable<T> addedItems) { lock (_itemsLock) { foreach (T item in addedItems) { _items.Add(item); } } } Don’t see it? Consider the following: Thread A, a background thread, is synchronizing changes made in-memory with the database: It already holds a lock on a class called "ChangeQueue". It calls HandleAdd(), passing in a list of changes.
Reactions / posts that link to this post
-
The Morning Brew #62
http://blog.cwa.me.uk/2008/03/31/the-morning-brew-62/Information Creating Windows Services - Sergio Pereira outlines a re-usable multi-task windows service. Custom Aggregations In LINQ - K. Scott Allen shows how you can implement your own custom aggregations and use them in LINQ Towards Bug-Free Code - Patrick Smacchia looks at 7 things that you can do to help reach a bug free state If you are using a loop, you’re doing it wrong - Chuck Jazdzewski shows LINQ alternatives for a number of places where most of us would be tempted to loop Dependency Injection With A Framework - Joel Ross does dependency injection using the castle Windsor framework LINQ Range Variable Oddness - Ian Griffiths compares range variables and iteration variables in a foreach loop Why Can’t I Access A Protected Member From A Derived Class, Part Two: Why Can I? - Eric Lippert tells of an interesting compiler bug in the C# 2 compiler CHAPTER 1: Problems and causes with the way we develop software - The first part of Dan Bunea’s Agile Mini Book series Locking Rule #71 - Paul Stovell takes a look at locking strategies.
More rising blog posts
-
Entertainment »
Mark Strong Joins 'Sherlock Holmes' -- Russell Crowe Still In? -
Business »
New Nokia N95-1 Firmware v30.0.015 Available -
Lifestyle »
Polly Guerin Talks About Writing Magazine Articles - Videos About Writing -
Politics »
Banning Books...Amazing that this is still a threat in the 21st Century -
Sports »
Beijing Paralympics on YouTube -
Technology »
Legacy Media Underreports Crowds at McCain/Palin Appearances
More rising news stories
-
Entertainment »
Daniel Radcliffe: 'I Love Broadway!' -
Business »
What rescue means for mortgage rates -
Lifestyle »
U.S. Outlines Fan-Fred Takeover -
Politics »
Afghan Civilian Deaths: A Rising Toll -
Sports »
Football: Manchester United game 'too soon' for Steven Gerrard's Liverpool return -
Technology »
Leo Hickman on whether the world ends on Wednesday
Recent posts from Paul Stovell says…
-
ORM: It’s time to do some real work
68 days ago -
Snippet: Ruby-like Hashes in C#
87 days ago -
Bindable LINQ: Snapshot Enumerators
88 days ago