The Old New Thing

Practical development throughout the evolution of Windows.

Latest posts

Lock-free algorithms: The opportunistic cache
Apr 14, 2011
Post comments count 0
Post likes count 0

Lock-free algorithms: The opportunistic cache

Raymond Chen
Raymond Chen

Suppose profiling reveals that a specific calculation is responsible for a significant portion of your CPU time, and instrumentation says that most of the time, it's just being asked to calculate the same thing over and over. A simple one-level cache would do the trick here. Of course, this isn't thread-safe, because if one thread is pre-empted inside the call to , then another thread will see values for and that do not correspond to each other. One solution would be to put a critical section around this code, but this introduces an artificial bottleneck: If the most recent cached result is , , and if two...

Lock-free algorithms: Update if you can I'm feeling down
Apr 13, 2011
Post comments count 0
Post likes count 0

Lock-free algorithms: Update if you can I'm feeling down

Raymond Chen
Raymond Chen

A customer was looking for advice on this synchronization problem: We have a small amount of data that we need to share among multiple processes. One way to protect the data is to use a spin lock. However, that has potential for deadlock if the process which holds the spinlock doesn't get a chance to release it. For example, it might be suspended in the debugger, or somebody might decide to use to nuke it. Any suggestions on how we can share this data without exposure to these types of horrible failure modes? I'm thinking of something like a reader takes the lock, fetches the values, and then checks at sta...

Overheard conversation fragment: I'm over here by the slot machines
Apr 12, 2011
Post comments count 0
Post likes count 0

Overheard conversation fragment: I'm over here by the slot machines

Raymond Chen
Raymond Chen

While on a trip to Las Vegas, I happened to overhear a woman talking on her mobile phone who, from her body language, was clearly trying to meet up with a friend. We were in the casino of one of the major hotels. She said, "I'm over here by the slot machines." Yeah, that narrows it down. I'll be heading to Vegas for the Niney Awards. If I don't see you at the ceremony, I'll meet you by the slot machines.

Lock-free algorithms: The try/commit/(try again) pattern
Apr 12, 2011
Post comments count 0
Post likes count 0

Lock-free algorithms: The try/commit/(try again) pattern

Raymond Chen
Raymond Chen

The singleton constructor pattern and the example we saw some time ago are really special cases of the more general pattern which I'll call try/commit/(try again). I don't know if this pattern has a real name, but that's what I'm calling it for today. The general form of this pattern goes like this: We calculate the desired new value based on the initial value, combining it with other values that vary depending on the operation you want to perform, and then use an to update the shared value, provided the variable hasn't changed from its initial value. If the value did change, then that means another thre...

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
Apr 11, 2011
Post comments count 0
Post likes count 0

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
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 ...

Patterns for using the InitOnce functions
Apr 8, 2011
Post comments count 0
Post likes count 0

Patterns for using the InitOnce functions

Raymond Chen
Raymond Chen

Letting the smart people do the work for you.

Lock-free algorithms: The singleton constructor (answer to exercises)
Apr 8, 2011
Post comments count 0
Post likes count 0

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

Raymond Chen
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:...

Lock-free algorithms: The one-time initialization
Apr 7, 2011
Post comments count 0
Post likes count 0

Lock-free algorithms: The one-time initialization

Raymond Chen
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 ...

Lock-free algorithms: Choosing a unique value (solutions)
Apr 6, 2011
Post comments count 0
Post likes count 0

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

Raymond Chen
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 ...