Channels ▼

Walter Bright

Dr. Dobb's Bloggers

It's Not Always Nice To Share

May 20, 2009

Back when multithreaded programming first appeared, people realized there was a problem with the C standard library. It relied on global variables like errno which could be read or set by any thread. In other words, a single instance of errno was shared among multiple threads.

This clearly wasn't going to work very well, so C runtime libraries got hacked up in various ways, and errno was turned into a macro that did some magic so that each thread saw their own version of errno. In other words, errno (and the other global state the C runtime library relied on) became thread local.

Over time this facility bubbled up to the application programmer in the form of a C compiler extension that allowed the declaration of thread-local global variables. Every compiler, of course, did it a different way and some didn't do it at all (and still don't, see gcc for the Mac OSX). (Fortunately, TLS (thread local storage) will be standardized for C++0x, so this situation should improve soon.)

It isn't just languages that have poor support for TLS, the operating systems do, too. Just try to write a garbage collector that can reliably find all the TLS allocated to a process, just try, my pretty. It can't be done on Windows, Linux, or Mac OSX, though they all fall short in different ways. Windows, for example, can provide you with a pointer to the base of the TLS but stubbornly won't give a clue as to how large it is. Linux will tell you how large it is, but won't tell you where it starts. As far as I can tell, Mac OSX has no concept of TLS. I'm hoping that the imminent standardization of TLS in C++0x will finally get some TLS lovin' from the OS developers.

But that isn't the real problem with TLS. The real problem is, nobody uses it. It's been in the D programming language compiler for the last year, and I know nobody uses it because I found some bugs in its implementation that prevented more than trivial uses of it. I can't even recall running across any code in C or C++ that uses it. I can't recall ever seeing TLS used in a C or C++ programming book.

And essentially, they're all wrong (!). Remember, global and static variables are implicitly shared, even static class members. As soon as you try to make your program multithreaded, or someone uses your fabulous library in a multithreaded environment, and no attempt was made to synchronize access to those globals, they all fail just like errno. Now you're faced with:

"I've got to make my code multithreaded. How do I even find all the statics and globals in my 100,000 line codebase?"

That's certainly a daunting problem, because you can't grep for them. Statics and globals can be declared in many diverse guises. You're stuck with either visually going through the code line by line (and then having to do it again for every maintenance update of the code) or if you're lucky you can instrument a compiler to flag every global.

The answer is to flip things around. Have statics and globals default to being TLS. To make a shared global, explicitly qualify it with "shared". This has immediate benefits:

  • All shared globals become easily greppable.
  • Only a small minority of globals actually need to be shared, so little code will need to be altered.
  • It's unlikely that someone would type "shared" accidentally, so the programmer who marked them as shared would be putting out a clear signal that the global was used for inter-thread communication. There's no more guessing about whether a global is meant to be shared or not.
  • If a global is meant to be shared, but is not marked as such, it will go into TLS. This means the shared communication won't work at all, and the bug will be apparent. Implicit sharing, on the other hand, leads to code that works one day and erratically fails the next.

Implicit TLS has just been implemented in D, and it already has uncovered at least a dozen latent sharing bugs in the runtime library.

Thanks to David Held and Jason House for reviewing this.

Related Reading


More Insights






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.
 


Video