C++ Reactive Programming
上QQ阅读APP看书,第一时间看更新

Asynchronous and Lock-Free Programming in C++

In the previous chapter, we looked at the threading library introduced by Modern C++ and various ways to create, manage, and synchronize threads. The way of writing code with threads is a rather low level and is prone to potential errors associated with concurrent code (deadlock, live-lock, and so on). Even though it is not noticed by many programmers, the Modern C++ language provides a standard memory model that helps to write concurrent code better. To be a concurrent programming language from the ground up, a language has to provide certain guarantees to the developer regarding memory access and the order in which things will be executed during runtime. If we are using constructs such as mutexes, condition variables, and futures to signal events, one doesn't need to be aware of the memory model. But awareness of the memory model and its guarantees will help us write faster concurrent code using lock-free programming techniques. Locks can be simulated using something called atomic operations, and we will look at this technique in depth.

As we discussed in Chapter 2, A Tour of Modern C++ and its Key Idioms, zero-cost abstraction remains one of the most fundamental principles of the C++ programming language. C++ is always a system programmer's language, and the standard committee managed to strike a good balance between higher-level abstraction mechanisms supported by the language and the ability to access lower-level resources to write system programs. C++ exposes atomic types and a set of associated operations to have fine-grained control over the execution of programs. The standard committee has published detailed semantics of the memory model, and the language has a set of libraries that help programmers to exploit them.

In the previous chapter, we learned how to synchronize actions in separate threads using condition variables. This chapter discusses the facilities provided by the standard library to perform task-based parallelism using futures. In this chapter, we will cover:

  • Task-based parallelism in C++
  • The C++ memory model
  • Atomic types and atomic operations
  • Synchronizing operations and memory ordering
  • How to write a lock-free data structure