Skip to Content
Effective Modern C++
book

Effective Modern C++

by Scott Meyers
November 2014
Intermediate to advanced
336 pages
9h 37m
English
O'Reilly Media, Inc.
Content preview from Effective Modern C++

Chapter 7. The Concurrency API

One of C++11’s great triumphs is the incorporation of concurrency into the language and library. Programmers familiar with other threading APIs (e.g., pthreads or Windows threads) are sometimes surprised at the comparatively Spartan feature set that C++ offers, but that’s because a great deal of C++’s support for concurrency is in the form of constraints on compiler writers. The resulting language assurances mean that for the first time in C++’s history, programmers can write multithreaded programs with standard behavior across all platforms. This establishes a solid foundation on which expressive libraries can be built, and the concurrency elements of the Standard Library (tasks, futures, threads, mutexes, condition variables, atomic objects, and more) are merely the beginning of what is sure to become an increasingly rich set of tools for the development of concurrent C++ software.

In the Items that follow, bear in mind that the Standard Library has two templates for futures: std::future and std::shared_future. In many cases, the distinction is not important, so I often simply talk about futures, by which I mean both kinds.

Item 35: Prefer task-based programming to thread-based.

If you want to run a function doAsyncWork asynchronously, you have two basic choices. You can create a std::thread and run doAsyncWork on it, thus employing a thread-based approach:

int doAsyncWork();

std::thread t(doAsyncWork);

Or you can pass doAsyncWork to std::async ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Effective C++, Third Edition

Effective C++, Third Edition

Scott Meyers
Embracing Modern C++ Safely

Embracing Modern C++ Safely

John Lakos, Vittorio Romeo, Rostislav Khlebnikov, Alisdair Meredith
C++ Software Design

C++ Software Design

Klaus Iglberger

Publisher Resources

ISBN: 9781491908419Errata Page