The Old New Thing

Practical development throughout the evolution of Windows.

Latest posts

Apr 11, 2011
Post comments count 0
Post likes count 1

Holding down the shift key when right-clicking lets you pin things to the Start menu even when you might have been better off not doing so

Raymond Chen

Holding the shift key when calling up a context menu is a convention for indicating that you want to see additional advanced options which are normally hidden. One of those options is Pin to Start menu. What is this doing as an extended command? The Pin to Start menu command normally appears on the context menu of a program or a shortcut to a program. The Start menu pin list was intended to be a launch point for programs, so if the item you pick isn't actually a program, the Pin to Start menu item will be hidden. Furthermore, only items on the local hard drive will show Pin to Start menu. This avoids ugliness ...

Apr 8, 2011
Post comments count 0
Post likes count 1

Patterns for using the InitOnce functions

Raymond Chen

Letting the smart people do the work for you.

Apr 8, 2011
Post comments count 0
Post likes count 1

Lock-free algorithms: The singleton constructor (answer to exercises)

Raymond Chen

A few days ago, I asked you to make an existing class multithread-safe. The class caches objects called which are indexed by a 32-bit ID. The cache is implemented as an array that dynamically resizes as more items are added to it. A naïve multithreaded version might use a slim reader-writer lock with shared access on reads, exclusive access on writes, and mixed access on the treacherous "create if it doesn't already exist" path. Let's see. First of all, the function doesn't allocate the memory for the cache until somebody actually tries to look something up. But duh, that's the whole point of the class:...

Apr 7, 2011
Post comments count 0
Post likes count 1

Lock-free algorithms: The one-time initialization

Raymond Chen

A special case of the singleton constructor is simply lazy-initializing a bunch of variables. In a single-threaded application you can do something like this: This works fine in a single-threaded program, but if the program is multi-threaded, then two threads might end up trying to lazy-initialize the variables, and there are race conditions which can result in one thread using values before they have been initialized: Observe that if the first thread is pre-empted after the value for is initially set, the second thread will think that everything is initialized and may end up using an uninitialized ...

Apr 6, 2011
Post comments count 0
Post likes count 1

Lock-free algorithms: Choosing a unique value (solutions)

Raymond Chen

Last time, I left a warm-up exercise consisting of a code fragment which tries to compute a unique process-wide value. Here it is again: It may be easier to enumerate what the function does right rather than what it does wrong. Um, the words are correctly-spelled. That's about it. Damien was the first to note that the author basically reimplemented . Poorly. As we saw earlier, the algorithm for performing complex calculations with interlocked functions is (capture, compute, compare-exchange, retry). But the above code didn't do any of these things. By failing to capture the values, the code is ...

Apr 6, 2011
Post comments count 0
Post likes count 1

Lock-free algorithms: The singleton constructor

Raymond Chen

The first half may be familiar to many (most?) readers, but there's an interesting exercise at the bottom. A very useful pattern for the Interlocked* functions is lock-free lazy initialization via . Yes, that's a really long function name, but it turns out every part of it important. This is a double-check lock, but without the locking. Instead of taking lock when doing the initial construction, we just let it be a free-for-all over who gets to create the object. If five threads all reach this code at the same time, sure, let's create five objects. After everybody creates what they think is the winning obje...

Apr 5, 2011
Post comments count 0
Post likes count 1

Lock-free algorithms: Choosing a unique value (warm-up)

Raymond Chen

Here's a snippet of code whose job is to generate a unique number within the process. Here's some reference reading to get yourself in the mood. Caution: It may or may not be useful. Criticize this code fragment.

Apr 4, 2011
Post comments count 0
Post likes count 1

Windows is not a .NET Framework delivery channel either

Raymond Chen

We learned a while ago that Windows is not an MFC delivery channel. And, since you asked, it's not a .NET Framework delivery channel either. If you're developing a program that uses the .NET Framework, you have to have a backup plan if the version of the .NET Framework you need is not installed on the computer. This might mean including a copy of the installer on your CD. It might mean redirecting the user to an appropriate download site. It might just mean telling the user, "This program requires version XYZ of the .NET Framework." Whatever you do, you need to do something. Windows XP didn't come with a...

Apr 1, 2011
Post comments count 0
Post likes count 0

The funniest joke I've ever told (to a three-year-old)

Raymond Chen

I've tested this joke on several children ages three and four, and it never fails. There were two kittens walking down the street, and one of them fell on its butt! I developed this joke for one of my honorary nieces. She had just started to learn about joke-telling and asked me to tell her a joke. One of the keys to joke-telling is to know your audience: A three-year-old won't have the attention span for a longer joke, and subtlety and observational humor won't work either. It's got to be short and direct. She thought kittens were cute, and falling on one's butt was funny, so I figured, hey, let me combine t...