The Old New Thing

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

A few days ago, I asked you to make an existing class multithread-safe. The class caches objects called SINGLETON­INFO 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 ...
Comments are closed.0 0
Code

Lock-free algorithms: The one-time initialization

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: // suppose that any valid values for a and b stipulate that // a ≥ 0 and b ≥ a. Therefore, -1 is never a valid value, // and we use it to mean "not yet initialized". int a = -1...
Comments are closed.0 0
Code

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

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: dwUniqueId = InterlockedCompareExchange(&g_dwUniqueId, g_dwUniqueId+1, g_dwUniqueId); It may be easier to enumerate ...
Comments are closed.0 0
Code

Lock-free algorithms: The singleton constructor

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 Interlocked­Compare­Exchange­Pointer­Release. Yes, that's a really long function name, but it turns out every part of it ...
Comments are closed.0 0
Code

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

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. dwUniqueId = InterlockedCompareExchange(&g_dwUniqueId, g_dwUniqueId+1, ...
Comments are closed.0 0
Code

Windows is not a .NET Framework delivery channel either

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 ...
Comments are closed.0 0
Code

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

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

The introduction of whimsical teasing in Comic Chat

A few months after my post on the sad demise of whimsical teasing in Comic Chat, I received a piece of email from none other than the author of Comic Chat, DJ Kurlander: I was the person that started the Comic Chat project in Microsoft Research and was responsible for that line, "This person is too lazy to create a profile entry." Not a ...