Showing tag results for Code

Apr 7, 2011
Post comments count0
Post likes count1

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

Code
Apr 6, 2011
Post comments count0
Post likes count1

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

Code
Apr 6, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The singleton constructor

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

Code
Apr 5, 2011
Post comments count0
Post likes count1

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

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

Code
Apr 4, 2011
Post comments count0
Post likes count1

Windows is not a .NET Framework delivery channel either

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

Code
Mar 31, 2011
Post comments count0
Post likes count2

Having an owner window from another process is tricky, but it's sometimes the right thing to do

Raymond Chen
Raymond Chen

A customer had a main program (let's call it A) and a helper program (let's call it B), and the customer wanted and wanted B to act like a modal dialog relative to A. When B is launched, we disable A's window and then call to simulate a modal dialog. How do we make sure that focus goes to B's window and not A's? We've found that if the...

Code
Mar 30, 2011
Post comments count0
Post likes count1

How do I get the title of a dialog from a dialog resource?

Raymond Chen
Raymond Chen

A customer submitted the following question: We are developing automated tests for our application. Among other things, our application uses property sheets, which means that the name of the tab is stored as the title of the dialog template resource. Since we want our automated tests to run on all language versions of our application, we don't wan...

Code
Mar 28, 2011
Post comments count0
Post likes count1

Why did Win32 define BOOL as a signed int instead of an unsigned int?

Raymond Chen
Raymond Chen

Igor Levicki wants somebody from Microsoft to explain why was defined as a instead of an . You don't need to work for Microsoft to figure this out. All the information you need is publically available. Quoting from K&R Classic, which was the operative C standards document at the time Windows was being developed: 7.6 Relational Operators ...

Code
Mar 25, 2011
Post comments count0
Post likes count1

How do I monitor, or even control, the lifetime of an Explorer window?

Raymond Chen
Raymond Chen

A customer wanted help with monitoring the lifetime of an Explorer window. We want to launch a copy of Explorer to open a specific folder, then wait until the user closes the folder before continuing. We tried launching a copy of Explorer with the folder on the command line, then doing a on the process handle, but the wait sometimes completes i...

Code
Mar 23, 2011
Post comments count0
Post likes count1

Function requirements are cumulative: If you fail to meet any of them, then all bets are off

Raymond Chen
Raymond Chen

A customer was having problems with the function: We are looking for a clarification of the behavior of . We have a thread that waits on two handles (call them and ) with , . Under certain conditions, we signal and close from another thread while the wait is in progress. This results in being returned from the wait call. MSDN is not clear o...

Code