RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Simpler Multithreading in C++0x : Page 5

The new standard will support multithreading, with a new thread library. Find out how this will improve porting code, and reduce the number of APIs and syntaxes you use.

Thread Local Data
The thread_local keyword can be used with any object declaration at namespace scope at local scope, and specifies that such a variable is thread local. Each thread thus has its own copy of that variable, and that copy exists for the entire duration of that thread. It is essentially a per-thread static variable, so each thread's copy of a variable declared at local scope is initialized the first time that particular thread passes through the declaration, and they retain their values until that thread exits:

    std::string foo(std::string const& s2)
        thread_local std::string s="hello";

        return s;
In this function, each thread's copy of s starts life with the contents "hello." Every time the function is called, the supplied string is appended to that thread's copy of s. As you can see from this example, this even works with class types that have constructors and destructors (such as std::string), which is an improvement over the pre-C++0x compiler extensions.

Thread-local storage isn't the only change to the concurrency support in the core language: There's also a brand new multi-threading aware memory model, with support for atomic operations.

The New Memory Model and Atomic Operations
Sticking to using locks and condition variables to protect your data, you won't need to worry about the memory model. The memory model guarantees to protect your data from race conditions—if you use locks correctly. You'll get undefined behavior if you don't.

If you're working at a really low-level and providing high-performance library facilities, then it's important to know the details—which are too complicated to go into here. For now, it's enough to know that C++0x has a set of atomic types corresponding to the built-in integer types and void pointers—and a template std::atomic<>—which can be used to create an atomic version of a simple user-defined type. You can look up the relevant documentation for the details.

That's All, Folks!
And that’s your whistle-stop tour of the new C++0x threading facilities, which has barely scratched the surface. There's much more to the library, with features such as thread IDs and asynchronous future values.

Anthony Williams is the Technical Director for Just Software Solutions Ltd., where he spends most of his time developing custom software for clients, mostly for Windows, and mostly C++. He is the maintainer of the Boost Thread library and is also a member of the BSI C++ Standards Panel. His latest book, "C++ Concurrency in Action: Practical Multithreading" is currently available in the Early Access Edition from Manning's web site.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date