Zbigniew Romanowski’s Post

𝗟𝗼𝗰𝗸-𝗙𝗿𝗲𝗲 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗯𝘆 𝗛𝗲𝗿𝗯 𝗦𝘂𝘁𝘁𝗲𝗿 𝗖𝗽𝗽𝗖𝗼𝗻 𝟮𝟬𝟭𝟰 "If you think using atomics is easy, you're either jaded, one of 50 people in the world — or not being careful enough." That opening line from Herb Sutter's iconic CppCon 2014 talk Lock-Free Programming (or, Juggling Razor Blades) sets the tone perfectly. This is a two-hour deep dive that every serious C++ engineer should have in their back pocket. Here are the core ideas that stuck with me: 𝗟𝗼𝗰𝗸-𝗳𝗿𝗲𝗲 𝗶𝘀 𝗮𝗻 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝘁𝗼𝗼𝗹, 𝗻𝗼𝘁 𝘁𝗵𝗲 𝗱𝗲𝗳𝗮𝘂𝗹𝘁 𝘀𝗲𝘁𝘁𝗶𝗻𝗴. Before using atomics, you should measure first. After writing your shiny new lock-free algorithm, measure again. Measure again. More often than not, the expected improvement simply isn't there. 𝗧𝗵𝗶𝗻𝗸 𝗶𝗻 𝘁𝗲𝗿𝗺𝘀 𝗼𝗳 𝘁𝗿𝗮𝗻𝘀𝗮𝗰𝘁𝗶𝗼𝗻𝘀. Model every member function and every locked region as a transition from one valid state to another. This mental model makes lock-free reasoning possible. A single atomic write can cause a whole graph of objects to come into existence for other threads. This is powerful, but only if you reason about it rigorously. 𝗟𝗼𝗰𝗸𝘀 𝗮𝗻𝗱 𝗮𝘁𝗼𝗺𝗶𝗰𝘀 𝗮𝗿𝗲 𝗻𝗼𝘁 𝗺𝘂𝘁𝘂𝗮𝗹𝗹𝘆 𝗲𝘅𝗰𝗹𝘂𝘀𝗶𝘃𝗲. Sutter explains producer-consumer designs in which ownership transitions between subsystems. In one phase, the transition is protected by a mutex; in the next phase, it is handed off via an atomic. The rule is simple: at any given moment in an object's lifetime, all threads must agree on how access is synchronized. This mechanism can change across phases. 𝗪𝗲𝗶𝗴𝗵𝘁-𝗳𝗿𝗲𝗲 → 𝗹𝗼𝗰𝗸-𝗳𝗿𝗲𝗲 → 𝗼𝗯𝘀𝘁𝗿𝘂𝗰𝘁𝗶𝗼𝗻-𝗳𝗿𝗲𝗲. These aren't just academic labels. They provide meaningful guarantees about system-wide throughput, starvation, and liveness. Knowing which level your algorithm achieves is important for reasoning about scalability under load. 𝗧𝗵𝗲 𝘀𝗶𝘇𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗿𝗶𝘁𝗶𝗰𝗮𝗹 𝘀𝗲𝗰𝘁𝗶𝗼𝗻 𝗶𝘀 𝗮 𝘀𝗰𝗮𝗹𝗮𝗯𝗶𝗹𝗶𝘁𝘆 𝗱𝗶𝗮𝗹. The mail-slots example illustrates this beautifully. Releasing a slot before or after doing the work is a subtle yet consequential decision, and the correct answer depends on whether the bottleneck is your producer or your consumers. The code looks almost identical either way. However, the performance implications are not. 💡 If you work in systems programming, high-performance infrastructure, or any field where concurrency matters, this talk is worth your time. It won't make lock-free programming easy. But it will help you understand exactly why it isn't. 𝗥𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲𝘀 🎥 Herb Sutter, "Lock-Free Programming (or, Juggling Razor Blades), Part I", CppCon 2014, 16 Oct 2014, https://lnkd.in/dXM3uS-v 🎥 Herb Sutter, "Lock-Free Programming (or, Juggling Razor Blades), Part II", CppCon 2014, 16 Oct 2014, https://lnkd.in/dQvQcSYx #cpp #concurrency #lockfree #cppcon #performanceengineering

To view or add a comment, sign in

Explore content categories