The Old New Thing

A function pointer cast is a bug waiting to happen

A customer reported an application compatibility bug in Windows. We have some code that manages a Win32 button control. During button creation, we subclass the window by calling . On the previous version of Windows, the subclass procedure receives the following messages, in order: We do not handle any of these messages and pass them...

No, you can't ask whether your notification icon is visible or not

A customer asked what started out as a simple question, but by now you know that this sort of simple question only raises new questions: Is there a way that we can detect that the user has hidden our notification icon? No, there is no way to ask whether your notification icon is hidden or visible. Users decide which icons they want to see, ...

Even if you have a lock, you can borrow some lock-free techniques

Even if you prefer to use a lock (after all, they are much easier to program), you can borrow some lock-free techniques. For example, consider this: There are some concerns here. First of all, there's the lock hierarchy issue: If reticulating a spline takes the geometry lock, that may violate our lock hierarchy. If the lock is a hot ...

Corrections to Patterns for using the InitOnce functions

Adam Rosenfield pointed out that it is not possible to fail an asynchronous initialization; if you pass when completing an asynchronous initialization, the function fails with . (Serves me right for writing an article the night before it goes up.) A more correct version is therefore In other words, the pattern is as follows: While I'...

Don't forget to include the message queue in your lock hierarchy

In addition to the loader lock, the message queue is another resource that people often forget to incorporate into their lock hierarchy. If your code runs on a UI thread, then it implicitly owns the message queue whenever it is running, because messages cannot be dispatched to a thread until it calls a message-retrieval function such as or...

Lock-free algorithms: The try/commit/(hand off) model

The last lock-free pattern for this week isn't actually lock-free, but it does run without blocking. The pattern for what I'll call try/commit/(hand off) is more complicated than the other patterns, so I'll start off by describing it in words rather than in code, because the code tends to make things more complicated. First, you take the ...