InfoQ

News

A Replacement for .NET's Flawed ReaderWriterLock

Posted by Jonathan Allen on Mar 05, 2007 06:21 AM

Community
.NET
Topics
.NET Framework
Tags
Multi-threading
Visual Studio 2007 will have a new lock called ReaderWriterLockSlim. According to Joe Duffy, in addition to being faster, it solves some of the nastier design flaws of its predecessor.
 
The ReaderWriterLock is known to be broken in some reentrancy cases and has a non-atomic upgrade which required checking the lock's WriterSeqNum.
 
The support upgradeable reads, ReaderWriterLockSlim has a new mode called UpgradeableRead. Only one thread may be in this mode at a time, so threads should quickly determine if the lock should be upgraded to a Write lock or downgraded to a ReadLock. If you downgrade, then the UpgradeableRead should be released immediately afterwards. This makes the design pattern for using the lock somewhat annoying, though it certainly beats deadlocks.
 
By default locks are not reentrant and attempts to recursively acquire a specific lock will result in an exception. Reentrancy can be specifically granted at construction time, presumably at a performance cost.
 
Conversions from Read locks to Write locks are specifically not allowed, as they would result in the same non-atomic upgrade that plagued the original ReaderWriterLock.
 
The ReaderWriterLockSlim isn't perfect however, and there are some serious reliability concerns when using it in hosted environment. For one, it doesn't participate in SQL Server's deadlock detection so you don't want to use it in libraries called from stored procedures. Secondly, thread aborts and out of memory exceptions may corrupt the lock and even peg the CPU. Since thread aborts shouldn't be used anyways and hard out of memory exceptions are always hard to recover from, this won't impact most applications.
 

No comments

Reply

Exclusive Content

Agile Project Management: Lessons Learned at Google

In this presentation filmed during QCon 2007, Jeff Sutherland, the creator of Scrum, talks about his visit at Google to do an analysis of Google's first implementation of Scrum.

AtomServer – The Power of Publishing for Data Distribution

In this article, Bryon Jacob and Chris Berry introduce AtomServer, their implementation of a full-fledged Atom Store based on Apache Abdera, which is now available as open source.

An Introduction to Virtualization

It is easy to think that virtualization applies only to servers. In reality the recent resurgence of the concept is also being applied to networking, storage, and application infrastructure.

REST Anti-Patterns

In this article, Stefan Tilkov explains some of the most common anti-patterns found in applications that claim to follow a "RESTful" design and suggests ways to avoid them.

Choosing between Routing and Orchestration in an ESB

In this article, Adrien Louis and Marc Dutoo discuss the differences and relative merits of using orchestration vs. routing in a typical ESB setup, and discuss various implementation options.

Enterprise Batch Processing with Spring

Wayne Lund discusses batch processing, Spring Batch objectives and features, scenarios for usage, Spring Batch architecture, scaling, example code, failures and retrying, and the future roadmap.

User Story Estimation Techniques

Developer Jay Fields draws on his experiences as a ThoughtWorks consultant to describe effective user story estimation techniques.

Security (CAS and OpenID) with Ruby

In this talk from QCon SF 2007, Justin Gehtland explains two open solutions to distributed identity and their Rails integration components: OpenID (using ruby-openid) and CAS (using rubycas-client).