Welcome to MSDN Blogs Sign in | Join | Help

High-performance multithreading is very hard

Among other things, you need to understand weak memory models.

Hereby incorporating by reference Brad Abrams' discussion of volatile and MemoryBarrier(). In particular, Vance Morrison's discussion of memory models is important reading.

(Though I think Brad is being too pessimistic about volatile. Ensuring release semantics at the store of "singleton" is all you really need - you want to make sure the singleton is fully constructed before you let the world see it. volatile here is overkill.)

Vance's message also slyly introduces the concepts of "acquire" and "release" memory semantics. An interlocked operation with "acquire" semantics prevents future reads from being advanced to before the acquisition. An interlocked operation with "release" semantics prevents previous writes from being delayed until after the release.

In the absence of explicitly-named memory semantics, the Win32 Interlocked* functions by default provide full memory barrier semantics. However, some functions, like InterlockedIncrementAcquire, forego the full memory barrier semantics and provide only acquire or release semantics.

Published Friday, May 28, 2004 6:59 AM by oldnewthing
Filed under:

Comments

# re: High-performance multithreading is very hard

Friday, May 28, 2004 9:02 AM by Scott Allen
Unfortunately the double check lock pattern was at one time presented as a best practice for singleton construction on MSDN, but I see there are some updates addressing the potential problem: (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp)

The pattern is everywhere in .NET software these days, including in this .Text blog software. .Text has a different threading problem in trying to avoid a lock for a performance gain:(http://odetocode.com/Blogs/scott/archive/2004/05/26/259.aspx)

# re: High-performance multithreading is very hard

Friday, May 28, 2004 9:53 AM by Mike Dimmick
See also Chris Brumme's Memory Model post for acquire and release information: http://blogs.msdn.com/cbrumme/archive/2003/05/17/51445.aspx

# re: High-performance multithreading is very hard

Friday, May 28, 2004 10:21 AM by Karl Barrus
Double checked locking is indeed broken: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

Scott Meyers was in town a few months ago and gave a talk on this very talk. Quite interesting.

# re: High-performance multithreading is very hard

Friday, May 28, 2004 10:33 AM by Raymond Chen
That's why you need the memory barrier with release semantics. Once you have that, you're safe again.

# re: High-performance multithreading is very hard

Friday, May 28, 2004 12:42 PM by Karl Barrus
Cool, I was just posting for information. Meyer's talk was about portable code so what was interesting was the fact so much is left out of the C++ standard. (The paper is on Java but Meyers did a C++ version of the talk).

# re: High-performance multithreading is very hard

Friday, May 28, 2004 1:29 PM by Alexei Zakharov
Slides from the talk Scott Meyers did a couple of months ago are here: http://www.nwcpp.org/Downloads/2004/DCLP_notes.pdf

He actually shows the protocol that has to be followed to get the double checked locking right in the weakest memory model possible (as defined in the C++ standard). I'm glad he did that because otherwise you have to dig that information from comp.programming.threading and it ain't easy.

# re: High-performance multithreading is very hard

Friday, May 28, 2004 4:11 PM by Pavel Lebedinsky
> Ensuring release semantics at the store of "singleton" is all you really need

You also need an acquire barrier between reading the "ready" flag and reading the data (the Scott Meyers link that Alexei posted mentions that).

I also agree with Meyers that the rule of thumb should be "if you access shared data, protect it with a lock".

It's only when you discover that locking is a bottleneck that you should start thinking about lock free algorithms, memory barriers etc. (And even then the right solution often is simply to change the design so that you lock less often or don't hold locks for too long).

# re: High-performance multithreading is very hard

Friday, May 28, 2004 4:50 PM by brian
I'm surprised to hear that MSDN was promoting double-check-lock as best practice. I recall first hearing about this anti-pattern in Java a few (maybe >= 4) years ago, and then how it was debunked as unsafe. Even after that, it was used in a Sun-supplied solution to the "Question of the Week" on their site (they've since fixed it: http://java.sun.com/developer/qow/archive/111/ ).

# It is not always the CPU

Tuesday, June 01, 2004 8:31 PM by Ishai's WebLog

# It is not always the CPU

Wednesday, June 02, 2004 1:40 AM by Ishai's WebLog

# re: High-performance multithreading is very hard

Tuesday, July 06, 2004 12:37 AM by Raymond Chen
Commenting on this entry has been closed.
New Comments to this post are disabled
 
Page view tracker