Showing tag results for Code

Jun 6, 2005
Post comments count0
Post likes count0

A quick puzzle about security and synchronization

Raymond Chen
Raymond Chen

This quick puzzle floated past one of our internal discussion groups. In Process B, the succeeds, but the returns immediately instead of waiting. Explain. [Correction: I confused the matter by passing as the third parameter, thereby creating an event that is initially signalled. Change it to so that the event is created non-signall...

Code
May 31, 2005
Post comments count0
Post likes count1

Using modular arithmetic to avoid timing overflow problems

Raymond Chen
Raymond Chen

In an earlier article, I presented a simple way of avoiding timing overflows which seemed to create a bit of confusion. The short version: Given a starting time start, an ending time end and an interval interval, the way to check whether the interval has elapsed is to use the expression . The naive expression suffers from integer overflow probl...

Code
May 30, 2005
Post comments count0
Post likes count0

You can't simulate keyboard input with PostMessage

Raymond Chen
Raymond Chen

Some people attempt to simulate keyboard input to an application by posting keyboard input messages, but this is not reliable for many reasons. First of all, keyboard input is a more complicated matter than those who imprinted on the English keyboard realize. Languages with accent marks have dead keys, Far East languages have a variety of Input Me...

Code
May 27, 2005
Post comments count0
Post likes count1

When is x/2 different from x>>1?

Raymond Chen
Raymond Chen

Everyone "knows" that the following pairs of expressions are equivalent: Too bad they aren't. In the C language standard, there is no requirement that the internal representation of signed integers be two's complement. All the permissible representations agree for positive numbers, but negative numbers can have different representations. If is ...

Code
May 25, 2005
Post comments count0
Post likes count1

The effect of SetCursor lasts only until the next SetCursor

Raymond Chen
Raymond Chen

Of course the effect of the function for a thread lasts only until that thread changes the cursor to something else. Any moron knows that, right? The tricky part is that the may come from an unexpected place. THe most common place people run into this is when they do something like this: This puts up the hourglass during the processing. ...

Code
May 24, 2005
Post comments count0
Post likes count0

Understanding ternary raster operations

Raymond Chen
Raymond Chen

It's perfectly logical, which doesn't mean that it's easy to understand. A ternary raster operation describes how three boolean values should combine to form an output boolean. I was going to write up a description but found that the existing documentation pretty much covers it, so I refer you to that. In particular, look at the table that expla...

Code
May 20, 2005
Post comments count0
Post likes count0

When is a window visible yet not visible?

Raymond Chen
Raymond Chen

Today, a quick puzzler. Consider the following code fragment: We just showed the window, certainly it is visible, right? Yet the assertion can fire (even in the absence of multi-threading). Why? Answer below - stop reading if you want to try to solve it yourself. Take a look at the function. If the specified windo...

Code
May 18, 2005
Post comments count0
Post likes count0

Loading the dictionary, part 5: Avoiding string copying

Raymond Chen
Raymond Chen

Looking at the profile for our program so far, 35% of the CPU time is spent copying strings around. Let's see if we can improve that. The best way to speed up copying strings is not to copy them in the first place. Using a in our structure forces the class to copy the string data, when all we really need to copy is the pointer and size infor...

Code
May 16, 2005
Post comments count0
Post likes count0

Loading the dictionary, part 4: Character conversion redux

Raymond Chen
Raymond Chen

Getting rid of was a big help, but 480ms is still not quite peppy enough. You need to respond to user actions within a tenth of a second for thing to seem responsive. Profiling the latest endeavor reveals that 40% of our CPU time is spent in . Some debugging reveals that ultimately calls but uses it to convert only one or two characters at ...

Code