RSS 2.0

Personal Info:

Joe Send mail to the author(s) is the lead developer and architect for Parallel Extensions to .NET, tinkers with type systems, and is an author and speaker.

Blogroll:
Other
News
 C|Net
 Kuro5hin
 The Register
Technology
 <?xmlhack?>
 Daily WTF
 DevX
 Hacknot
 Java Today
 Microsoft Top 10 Downloads
 MSDN
 MSDN: "Longhorn"
 MSDN: XML Developer Center
 Slashdot
 Techdirt
 theserverside.com
 W3C
 Web Pages That Suck
 XML Cover Pages
 XML Journal
 xml.com
Technology Blogs
 Aaron Skonnard [PluralSight]
 Adam Bosworth [Google]
 Andy Rich [MS/C++]
 Arpan Desai [MS/XML]
 BCL Team [MS]
 Bill Clementson [Lisp]
 Bill de hÓra
 Bruce Eckel [J]
 Bruce Tate [J]
 Casey Chestnut
 Cedric Beust [Google]
 Chris Anderson [MS/Avalon]
 Chris Lyon [MS]
 Christian Weyer
 Clemens Vasters [newtelligence]
 Craig Andera [PluralSight]
 Dan Sugalski [Parrot]
 Daniel Cazzulino
 Dave Chappel
 Dave Roberts [Lisp]
 Dave Thomas [PragProg]
 Dave Winer
 Dion Almaer [J]
 Don Demsak
 Doug Purdy [MS/Indigo]
 Drew Marsh
 Eric Gunnerson [MS]
 Eric Rudder [MS]
 Eric Sink
 Fritz Onion [PluaralSight]
 Gavin King [J/Hibernate]
 Grady Booch [IBM]
 Hervey Wilson [MS/Indigo]
 Hillel Cooperman [MS/Shell]
 Howard Lewis Ship [J/Apache]
 Ingo Rammer [PluralSight]
 James Gosling [J/Sun]
 James Strachan [J/Groovy]
 Jason Matusow [MS/OSS]
 Jeffrey Schlimmer [MS/Indigo]
 Joe Beda [Google]
 Joel Spoelsky
 Jon Udell
 Josh Ledgard [MS/Evang]
 Joshua Allen [MS]
 Lambda
 Larry Osterman [MS]
 Maoni Stephens [MS/CLR]
 Mark Fussell [MS/XML]
 Martin Fowler
 Martin Gudgin [MS/Indigo]
 Me
 Michael Howard [MS]
 Miguel de Icaza [Mono]
 Mike Clark
 Omri Gazitt [MS/Indigo]
 Pat Helland [MS/PAG]
 Pinku Surana
 Raymond Chen [MS]
 Rich Lander [MS/CLR]
 Rob Howard
 Rob Relyea [MS/Avalon]
 Robert Cringely
 S. Somasegar [MS/DevDiv]
 Sam Gentile
 Scoble [MS/Evang]
 Scott Guthrie [MS/WebNet]
 Scott Hanselman
 Sean McGrath [J]
 Simon Fell
 Stanley Lippman [MS/C++]
 Steve Maine
 Steve Swartz [MS/Indigo]
 Steve Vinoski
 Steven Clarke [MS/Usability]
 Stuart Halloway
 Ted Leung
 Ted Neward [DM]
 Tim Bray [Sun]
 Tim Ewald [Mindreef]
 Tim O'Reilly
 Werner Vogels [Amazon]
 Wintellect
 Yasser Shohoud [MS/Indigo]
Top 20
 Brad Abrams [MS/CLR]
 Chris Brumme [MS/CLR]
 Chris Sells [MS/Ultra]
 Cyrus Najmabadi [MS/C#]
 Dominic Cooney [MS/XAF]
 Don Box [MS/Ultra]
 Don Syme [MS/R]
 Guido van Rossum [Python]
 Herb Sutter [MS/C++]
 Ian Griffiths
 Jason Zander [MS/CLR]
 Jim Hugunin [MS/CLR]
 Joel Pobar [MS/CLR]
 Krzysztof Cwalina [MS/CLR]
 Patrick Logan
 Paul Graham
 Rico Mariani [MS/CLR]
 Rory Blyth [MS/DN]
 Sam Ruby
 Wesner Moise
VC/Business Blogs
 Ed Sim
 Fred Wilson
 Jonathan Schwartz [J/Sun]
 Lawrence Lessig [Stanford]
 Mark Cuban
 Michael Hyatt
 Pierre Omidyar
 Ross Mayfield
 VentureBlog
 Weekly Read
Wine, Food & Tea
 The Silk Road of Wine
 Vinography: a wine blog
 Wine Whys

Disclaimer:
The content of this site are my own personal opinions and do not represent my employer's view in anyway.

© 2009, Joe Duffy

 
 Thursday, March 29, 2007

One of the motivations of doing a new reader/writer lock in Orcas (ReaderWriterLockSlim) was to do away with one particular scalability issue that customers commonly experienced with the old V1.1 reader/writer lock type (ReaderWriterLock).  The basic issue stems from exactly how the lock decides when (or in this case, when not) to wake up waking writers.  Jeff Richter’s MSDN article from June of last year highlights this problem.  This of course wasn't the primary motivation, but it was just another straw hanging off the camel's back.

Contrast some choice behavior exhibited by the two lock types:

  • Both ReaderWriterLock and ReaderWriterLockSlim will block new readers from acquiring the lock as soon as a writer begins waiting to enter.  That means that as soon as all active readers exit the lock, the writer will be awoken and allowed to enter the lock.  (For all intents and purposes, by the way we treat upgrade and write locks the same.)
  • When a write lock is released, the ReaderWriterLock type will wake up all waiting readers, even if there are waiting writers.  Again, new readers are blocked and once this awoken batch of read locks exit the lock again, the next writer in line is awoken.
  • When a write lock is released, the ReaderWriterLockSlim type will wake up a waiting writer instead of readers.  Readers may only proceed once there are no longer any waiting writers.

These last two points illustrate the basic issue with the old lock.  (And the new one, too, to be brutally honest.)  If a large number of writers is waiting to enter the ReaderWriterLock, each will be staggered by the amount of time it takes for all intervening readers to enter the lock, do their work, and exit.  This can send the wait time for writers through the roof.

To further illustrate the point, imagine we have two writers (W0 and W1) and two readers (R0 and R1), each of which enters the lock, does some work for 1 unit of time, exits, and then goes back around and tries to acquire the lock again:

Thread  Arrival  Enter  Exit
W0      0        0      1
W1      0        3      4
R0      0        1      2
R1      0        1      2
W0      1        5      6
W1      4        8      9
R0      2        4      5
R1      2        4      5
...
R0      5        6      7
R1      5        6      7

Notice that the writers have to wait for a very long time to be serviced in comparison to the readers.  If more and more writers show up, this problem becomes magnified, regardless of how many readers there are or the ratio of readers to writers.

The new lock doesn’t suffer from this same problem.  But it does suffer from a different one: possible starvation of readers if there is always a writer arriving or waiting at the lock.  As Jeff mentions in his MSDN article, most reader/writer locks work best when the ratio of reads to writes is high.  In the above example, though, the readers actually would never get to enter the lock:

Thread  Arrival  Enter  Exit
W0      0        0      1
W1      0        1      2
R0      0        ??      ??
R1      0        ??      ??
W0      1        2      3
W1      2        3      4
...
W0      3        4      5
W1      4        5      6

So which is better?  If writers are less frequent in your scenario—as they usually are—then the new lock will probably fit the bill.  If not, you might run into troubles with the new one.

We had originally planned to allow you to configure the contention policy.  In fact, if you picked up an earlier Orcas CTP, you probably noticed the ReaderWriterLockSlim constructor that took an enumeration value specifying the contention policy: PrefersReaders, PrefersWritersAndUpgrades, and Fifo.  This simply added too much complexity for the short timeframe of the Orcas release, so it recently silently disappeared.

Though it’s the hardest to implement, I do think FIFO (or some heuristic approximation thereof) is the right answer here.  Block new readers once a writer begins waiting.  When the last reader exits, wake up the waiting writer.  When the writer exits, wake up the next n contiguous waiting readers (all readers between the exiting writer and the next writer in the wait queue, if any) or the next writer (if the writer is next in the wait queue).  Or, as noted, some approximation of this logic, since it could be fairly costly to orchestrate all the bookeeping, particularly the event waiting and signaling.  But a FIFO-like ordering ensures some strong correlation between arrival time and relative wait time, which, I think, is what most people expect and desire.  There are of course convoy problems that can happen when strict FIFO ordering is used, so I would expect we would still allow (some) arriving requests to pass others in line.

This last suggestion is actually quite similar to what the new SRWLOCK in Vista does.  ReleaseSRWLockShared and ReleaseSRWLockExclusive signal the next threads in line based on a wait queue structure, without any sort of “prefers readers over writers” (or vice versa) policy.  But that’s a topic for a separate day.

3/29/2007 1:15:41 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [2]
Tracked by:
http://9lq-free-porn.info/58054330/index.html [Pingback]
http://9lo-free-porn.info/37431705/index.html [Pingback]
http://9lq-free-porn.info/48847255/korn-porn.html [Pingback]

 

Recent Entries:

Search:

Browse by Date:
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Browse by Category:

Notables:

Currently Up To:

Reading...

Listening...

Watching...