Atomics – Safely Reclaiming Memory

In the previous chapter, we discussed the atomic primitives available to the Rust programmer, implementing higher-level synchronization primitives and some data structures built entirely of atomics. A key challenge with atomic-only programming, compared to using higher-level synchronization primitives, is memory reclamation. It is only safe to free memory once. When we build concurrent algorithms only from atomic primitives, it's very challenging to do something only once and keep performance up. That is, safely reclaiming memory requires some form of synchronization. But, as the total number of concurrent actors rise, the cost of synchronization dwarfs the latency or throughput benefits of atomic programming.

In this chapter, we will discuss three techniques to resolve the memory reclamation issue of atomic programming—reference counting, hazard pointers, and epoch-based reclamation. These methods will be familiar to you from previous chapters, but here we will do a deep-dive on them, investigating their trade-offs. We will introduce two new libraries, conc and crossbeam, which implement hazard pointers and epoch-based reclamation, respectively. By the close of this chapter, you should have a good handle on the three approaches presented and be in a good place to start laying down production-quality code.

By the end of this chapter, we will have:

  • Discussed reference counting and its associated tradeoffs
  • Discussed the hazard pointer approach to memory reclamation
  • Investigated a Treiber stack using the hazard pointer approach via the conc crate
  • Discussed the epoch-based reclamation strategy
  • Done a deep investigation of the crossbeam_epoch crate
  • Investigated a Treiber stack using the epoch-based reclamation approach
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.192