Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Clay Breshears

Dr. Dobb's Bloggers

The Tell-Tale Compiler

May 10, 2012

In my last post, I warned about the need to be aware of the memory models of both the programming language and the hardware when writing and executing threaded applications. In this post, I want to remind you also to consider issues that your parallel code can encounter from the compiler.

More Insights

White Papers

More >>


More >>


More >>

Normally I think of a compiler as being a "friend" to me and my application. I'm old enough to remember assembly language programming being taught to budding Computer Science students. This topic has disappeared, for the most part, from modern CS curriculum. It might be featured in a computer architecture and organization course, but no in-depth study is offered. Why? Compilers have gotten so good at code optimizations that trying to eke out a slightly more optimal execution for a small portion of your application by inlining some assembly language is typically more trouble than it is worth. And it is that optimization from the compiler that can trip you up in parallel code.

For example, let me remind you of the example I previously used to illustrate how the memory model of a processor could result in incorrect execution. I want to update a shared variable in one thread, threadZero, and another thread, threadOne, is going to read that newly updated value. To force the ordered execution I use a shared flag as synchronization, as shown in the following code fragment.

// Shared declarations
int Done = FALSE;
int N;

void threadZero(void *pArg)
  . . .
  N = SomeLocalValue;
  Done = TRUE;
  . . .

void threadOne(void *pArg)
  . . .
  while (!Done) {}   // spin-wait
  SomeOtherLocalValue = N;
  . . .

If I use a smart compiler, it could notice that the value of Done is not modified in the spin-wait loop of threadOne, or is loop invariant. In other words, the compiler is free to assume that the value of Done does not change in the course of executing the (empty) while loop when executing the function. Thus, the compiler is justified in transforming the relevant code in threadOne to the following:

void threadOne(void *pArg)
  . . .
  int tmp = Done; 
  while (!tmp) {}   // spin-wait
  SomeOtherLocalValue = N;
  . . .

Unless the value of Done is non-zero when threadOne executes the code above, threadOne will enter into an infinite loop. If you were to analyze this code for data races with some software tool, it would point out that there is a race on the Done variable. But, you already know about that data race; in fact, the original source code relies on the data race to work correctly. The compiler has turned what should have been a benign data race into a fatal deadlock situation. (Here is more evidence that even benign data races should be protected with mutually exclusive access synchronization.)

Related Reading

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.