Modern C++23 QuickStart Pro: Advanced programming including variadic templates, lambdas, async IO, multithreading and thread sync
By Jarek Thalor
()
About this ebook
Learn the latest features of C++23 with Modern C++ 23 QuickStart Pro, the perfect book for experienced developers who want to expand their knowledge and skills. This book takes a hands-on approach, providing rapid learning through real-world examples and scenarios that address complex programming challenges in C++.
The book beg
Related to Modern C++23 QuickStart Pro
Related ebooks
Mastering C++ Swiftly Rating: 0 out of 5 stars0 ratingsMastering C: A Comprehensive Guide to Proficiency in The C Programming Language Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsMastering C++ Network Automation Rating: 0 out of 5 stars0 ratingsUltimate Tailwind CSS Handbook: Build sleek and modern websites with immersive UIs using Tailwind CSS Rating: 0 out of 5 stars0 ratingsVersion Control with Git: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsSpring Boot 3.0 Crash Course Rating: 0 out of 5 stars0 ratingsTerraform Unleashed: An In-Depth Exploration and Mastery Guide Rating: 0 out of 5 stars0 ratingsRacket Unleashed: Building Powerful Programs with Functional and Language-Oriented Programming Rating: 0 out of 5 stars0 ratingsMastering Clojure: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsProfessional Guide to Linux System Programming: Understanding and Implementing Advanced Techniques Rating: 0 out of 5 stars0 ratingsCSS Mastery: Styling Web Pages Like a Pro Rating: 0 out of 5 stars0 ratingsTypeScript Programming In Action: Code Editing For Software Engineers Rating: 0 out of 5 stars0 ratingsComputer Data Rating: 0 out of 5 stars0 ratingsDynamic Programming in Java: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsMariaDB Cookbook Rating: 0 out of 5 stars0 ratingsAdvanced Julia Programming: Comprehensive Techniques and Best Practices Rating: 0 out of 5 stars0 ratingsBasics of Python Programming: Learn Python in 30 days (Beginners approach) - 2nd Edition Rating: 0 out of 5 stars0 ratingsFinding Data Patterns in the Noise: A Data Scientist's Tale Rating: 0 out of 5 stars0 ratingsFrontend Software Development and Best Practices: A Concise Guide Rating: 5 out of 5 stars5/5Apache Hive Cookbook Rating: 0 out of 5 stars0 ratingsDocker Networking Cookbook Rating: 0 out of 5 stars0 ratingsiWork for Beginners: [3 in 1] The Most Updated All-in-One Guide for MAC OS X and iOS Including Pages, Numbers, and Keynote Rating: 0 out of 5 stars0 ratingsThe Cloud at Your Service: The when, how, and why of enterprise cloud computing Rating: 3 out of 5 stars3/5PostgreSQL 11 Administration Cookbook: Over 175 recipes for database administrators to manage enterprise databases Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python for Data Science For Dummies Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Algorithms For Dummies Rating: 4 out of 5 stars4/5Beginning Programming with Python For Dummies Rating: 3 out of 5 stars3/5
Reviews for Modern C++23 QuickStart Pro
0 ratings0 reviews
Book preview
Modern C++23 QuickStart Pro - Jarek Thalor
Modern C++23 QuickStart Pro
Advanced programming including variadic templates, lambdas, async IO, multithreading and thread sync
Jarek Thalor
Preface
Learn the latest features of C++23 with Modern C++23 QuickStart Pro, the perfect book for experienced developers who want to expand their knowledge and skills. This book takes a hands-on approach, providing rapid learning through real-world examples and scenarios that address complex programming challenges in C++. The book begins by demonstrating the power of variadic templates and how to use them for dynamic function signatures. After becoming familiar with fold expressions for argument handling, you will then explore std::tuple and std::variant for handling heterogeneous data. The book then covers advanced function morphing with parameter packs and shape-shifting lambdas, as well as dynamic programming techniques. It also teaches complex function overloading and high-level thread orchestration using futures, promises, and callables.
Next, we'll go over some low-level IO operations, such as controlling IO streams, efficiently handling file descriptors, and directly manipulating files. You will then learn how to optimize memory management with shared, unique, and weak pointers, and how to engineer memory performance with custom allocators and cache-aware programming. You will learn advanced synchronization, including atomic operations, mutexes, locks, and thread pools, as well as lock-free data structures for peak performance. In addition, this book covers optimal integer and floating-point operations, arbitrary precision arithmetic, precise rounding with fixed-point arithmetic, and high-performance computation using math constant integration.
In this book you will learn how to:
Utilize C++23 variadic templates for dynamic function signatures.
Use fold expressions to simplify complex function operations and argument handling.
Manage heterogeneous data in high-performance applications with std::tuple and std::variant.
Use parameter packs and perfect forwarding to create flexible function signatures.
Use shape-shifting lambdas for flexible argument patterns.
Master file manipulation and stream management to create efficient low-level IO systems.
Customize memory management with unique, shared, and weak pointers to control resources.
Boost parallel processing with mutexes, locks, and thread pools.
Create lock-free data structures to reduce locking overhead in concurrent systems.
Use mathematical constants and precise rounding to improve numerical computations.
GitforGits
Prerequisites
This practical book is ideal for advanced C++ users who want to maximize the benefits of C++23. Knowing C++ 11 and above is preferrable to get the most out of this book.
Codes Usage
Are you in need of some helpful code examples to assist you in your programming and documentation? Look no further! Our book offers a wealth of supplemental material, including code examples and exercises.
Not only is this book here to aid you in getting your job done, but you have our permission to use the example code in your programs and documentation. However, please note that if you are reproducing a significant portion of the code, we do require you to contact us for permission.
But don't worry, using several chunks of code from this book in your program or answering a question by citing our book and quoting example code does not require permission. But if you do choose to give credit, an attribution typically includes the title, author, publisher, and ISBN. For example, Modern C++ QuickStart Pro by Jarek Thalor
.
If you are unsure whether your intended use of the code examples falls under fair use or the permissions outlined above, please do not hesitate to reach out to us at [email protected].
We are happy to assist and clarify any concerns.
Prologue
Years ago, when I initially came across C++, I was astounded by how precisely it could mold complicated systems. C++ is clearly not your average programming language, as I discovered during my explorations. It's a tool for developing robust, high-performance applications. I was able to increase my level of control and flexibility by making use of new features introduced to the language as it developed. Programming in the modern era requires power and efficiency, and C++23 signifies the beginning of that era. This book captures that demand. To take your C++ skills to the next level, Modern C++23 QuickStart Pro
is a must-have book. This book is not solely about concepts. It's about creating and breaking down real-world examples. You'll discover techniques that you can immediately apply to your own work. I resolved to devote myself fully to learning the language's newest features and to avoiding theoretical tangents for the time being.
We'll start with one of C++'s most fascinating features: variadic templates. If you've ever struggled with managing dynamic, unknown numbers of arguments in your functions, variadic templates are the answer. Next, we'll look at fold expressions, which are an elegant way to streamline operations with variable parameters. This approach eliminates the need for boilerplate code, allowing you to focus on logic. Combining these ideas into highly adaptable and reusable functions is something I'll demonstrate in the first few chapters.
Using types will be a whole new ballgame in C++23. When it comes to managing and manipulating different data types, std::tuple and std::variant will be your new best friends. These features will allow you to maintain type safety while also making your code readable and performant, whether you're working with databases, processing data streams, or managing complex systems. In the end, we'll cover complex numerical operations, such as arbitrary precision and fixed-point arithmetic with controllable precision. In domains where even a minor rounding error can lead to major issues, such as scientific computing, real-time systems, and finance, these ideas are fundamental for achieving success.
Far from providing only superficial explanations, this book delves deep. I will teach you how to push the boundaries of what is possible in C++23. We'll progress from understanding concepts to mastering the most advanced features of the language. You will also discover the fascinating world of low-level I/O. Direct file manipulation and IO streams will push the boundaries of what C++ can do for developers in terms of hardware control. These sections will change the way people approach performance-critical applications, where efficient IO means the difference between success and failure.
This book also covers memory management. I've seen countless projects fail because of memory leaks, inefficient use, or poor resource management. These issues can be avoided. I demonstrate the usage of shared pointers, weak pointers, and unique pointers and also cover custom allocators and cache-aware programming. You will gain a better understanding of how to allocate
and manage resources while learning to build memory systems that outperform conventional methods. Finally, we must address multithreading; otherwise, any study of modern C++ will be lacking. I'll demonstrate how to easily spawn and synchronize threads, use mutexes and locks to avoid deadlocks, and optimize parallel execution with thread pools. I'm especially excited to show you how to use lock-free data structures to avoid the overhead of locking in highly concurrent environments. Finally, we'll look at advanced numeric operations, including arbitrary precision arithmetic and fixed-point arithmetic, which provides more precise control. These ideas are essential in areas where even a little rounding mistake can have a major impact, such as scientific computing, real-time systems, and the financial sector.
Scratching the surface isn't what this book is all about. Assisting you in becoming proficient enough to explore the limits of C++23 is the main objective of this book.
Copyright © 2024 by GitforGits
All rights reserved. This book is protected under copyright laws and no part of it may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without the prior written permission of the publisher. Any unauthorized reproduction, distribution, or transmission of this work may result in civil and criminal penalties and will be dealt with in the respective jurisdiction at anywhere in India, in accordance with the applicable copyright laws.
Published by: GitforGits
Publisher: Sonal Dhandre
www.gitforgits.com
Printed in India
First Printing: September 2024
Cover Design by: Kitten Publishing
For permission to use material from this book, please contact GitforGits at [email protected].
Content
Preface
GitforGits
Acknowledgement
Chapter 1: Potential of Variadic Power in C++23
Overview
Power of Variadic Templates
Background
What Makes Variadic Templates Powerful?
Recursive Template Instantiations
Leveraging Parameter Packs
Applicability and Use-cases
Sculpt Dynamic Functions with Fold Expressions
Initial Program with Variadic Templates
Simplifying with Fold Expressions
Collapsing Argument Lists for Aggregation
Eliminate Boilerplate Code
Expert Commanding ‘std::tuple’ and ‘std::variant’
Managing Heterogeneous Data with std::tuple
Manipulating and Unpacking Tuples
‘std::variant’ for Type-Safe Unions
Transforming and Accessing ‘std::variant’ Values
Combining ‘std::tuple’ and ‘std::variant’
Master Parameter Packs for Flexible Function Signatures
Putting Parameter Packs into Action
Perfect Forwarding and Universal References
Perfect Forwarding
Universal References
Perfect Forwarding with Parameter Packs
Combining Multiple Types with Parameter Packs
Summary
Chapter 2: Morphing Functions and Lambdas
Overview
Morph Functions with Parameter Packs
Dynamically Morphing Functions with Parameter Packs
Creating Reusable and Flexible APIs
Perfect Forwarding with Parameter Packs
Flexible APIs with Template Metaprogramming
Variable Argument Handling using Shape-shifting Lambdas
Introduction to Lambdas
Utilizing Shape-shifting Lambdas
Lambdas as Flexible and Dynamic Callable Objects
Shape-shifting Lambdas with Other Callable Objects
Hack Code with Function Overloading
Basic Function Overloading
Combining Variadic Templates and Function Overloading
Handling Complex Overloading Cases
Overload Resolution with Template Metaprogramming
Crafting Type-safe Solutions with Perfect Forwarding
Dynamic Functions with ‘std::function’ and Callables
Introduction to ‘std::function’
Storing and Passing Dynamic Functions
Functors with ‘std::function’
Passing Functions as Parameters
Storing Functions in Containers
‘std::function’ with Member Functions
Summary
Chapter 3: Taming Low-Level IO Operations
Overview
Dominate File Descriptors with Precision
Opening and Managing File Descriptors
Controlling File Access and Non-Blocking Operations
Reading and Writing with File Descriptors
Handling File System Resources with Precision
Push Boundaries with Direct File Manipulation
Reading and Writing Bytes at the Raw Level
Managing Buffers for Direct File Manipulation
Seeking and Manipulating File Offsets
Direct Interaction with Storage Devices
Streamline Low-Level IO Streams
Overview of Low-Level IO Streams
Sample Program: Wrapping File Descriptors in Stream Buffers
Reading with ‘underflow()’
Writing with ‘overflow()’
Flushing with ‘sync()’
Advanced Stream Buffers and IOStream Customization
Stream Buffer Management for Performance
Customizing ‘std::streambuf’ for Complex I/O
Summary
Chapter 4: Mastering Buffering and Async IO
Overview
Dive into Deep Buffering Mechanics
Buffering Techniques
Full Buffering
Line Buffering
No Buffering
Sample Program: Tuning Buffer Sizes for Optimized I/O
Excel Async I/O
Overview of Asynchronous I/O
Sample Program: Non-blocking I/O using ‘select()’
Sample Program: Non-blocking I/O with Network Sockets
Push Data Transfer Limits with Direct IO
What is Direct I/O?
Sample Program: Using Direct I/O in File Operations
Using Memory-Mapped I/O
Zero-Copy Mechanisms
I/O Performance with Asynchronous Streams
Coroutines Overview
Sample Program: Asynchronous I/O with Coroutines
Summary
Chapter 5: Outperforming Memory Management
Overview
Hack the Memory Layout
Memory Alignment
Padding Elimination
Cache-Line Optimization
Sample Program: Optimizing Memory Layout
Check the Size and Alignment
Optimize the Memory Layout
Cache-Line Optimization
Alignment and Compiler-Specific Optimizations
Push ‘std::string’ and ‘std::string_view’
‘std::string’ vs. ‘std::string_view’
Sample Program: Integrate ‘std::string’ and ‘std::string_view’
Sample Program: Efficient String Parsing with ‘std::string_view’
When to Use?
Exploit Unique, Shared, and Weak Pointers
Define the Particle and ParticleSystem with Smart Pointers
Simulating Complex Interactions
Simulation and Resource Management
Summary
Chapter 6: Engineering Memory Performance
Overview
Engineer Custom Allocators
Process of Customizing Allocators
When to Use Custom Allocators?
Sample Program: A Simple Custom Allocator
Unlock Performance with Cache-aware Programming
How the CPU Cache Works?
Sample Program: Applying Cache-aware Programming
Structuring Data for Cache Efficiency
Optimizing for Cache Efficiency
Cache-aware Data Traversal
Optimize Memory with Placement New and Aligned Allocations
Introduction to Placement New
Introduction to Aligned Allocations
Sample Program: Placement New and Aligned Allocations
Define the Particle Structure with Alignment
Allocating Aligned Memory
Verifying Alignment
Summary
Chapter 7: Advanced Multithreading for Experts
Overview
Spawn and Command Threads Effortlessly
Basic Thread Creation and Management
Advanced Thread Management and Synchronization
Sample Program: Spawning Multiple Threads
Thread Safety and Data Races
Spawning Threads for High-Performance Apps
Thread Pooling
Task-based Parallelism
Load Balancing
Sample Program: Dynamic Thread Spawning with ‘std::async’
Unlock Parallelism with Mutexes and Locks
What are Mutexes and Locks?
Sample Program: Protecting Shared Data with a Mutex
Sample Program: Avoiding Deadlock with Multiple Mutexes
Avoiding Deadlock with ‘std::lock()’
Sample Program: Avoid Race Condition using ‘std::unique_lock’
Tame Thread Communication with Condition Variables
How Condition Variables Work?
Sample Program: Using Condition Variable
Sample Program: Multiple Producers and Consumers with Condition Variables
Balance Load with Thread Pools
Background
Working of Thread Pool
Sample Program: Designing a Thread Pool
Sample Program: Implementing a Task for Thread Pool
Summary
Chapter 8: Thread Synchronization and Atomic Mastery
Overview
Thread Synchronization for Deadlocks
Underlying Causes of Deadlocks
Deadlocks Use-cases
Database Deadlocks
Operating Systems
Multithreaded Applications
Deadlocks Avoidance Techniques
Resource Ordering
Lock Hierarchies and Hierarchical Locking
Deadlock Detection with Timed Locks
Thread Priority Inversion and Priority Inheritance
Execute Atomic Operations with Precision
Atomic Operations Overview
Sample Program: Using Atomic Operations in a Counter
Advanced Atomic Operations using Compare-and-Swap
Orchestrate Threads with Futures, Promises, and Tasks
Futures, Promises, and Tasks
Sample Program: Asynchronous Computation with Futures and Promises
Using std::async for High-Level Thread Orchestration
Combining Futures, Promises, and Tasks
Build Lock-Free Data Structures for Ultimate Performance
What Makes Data Structures Lock-Free?
Sample Program: Building a Lock-Free Stack
Summary
Chapter 9: Turbocharging Floats and Ints
Overview
Arbitrary Precision Arithmetic
Concept and Its Use
Benefits of Arbitrary Precision Arithmetic
Sample Program: Planetary Orbit Simulation
Sample Program: Arbitrary Precision in Financial Calculations
Warp Arithmetic Operations into High Gear
Efficient Algorithms for Mathematical Operations
Optimizing Matrix Multiplication
Optimizing Matrix Multiplication Algorithm
Optimizing Trigonometric Operations
Take Control with Fixed-Point Arithmetic
Basics of Fixed-Point Arithmetic
Performance Benefits of Fixed-Point Arithmetic
Implementing Fixed-Point Arithmetic
Sample Program: Fixed-Point Arithmetic for Temperature Control
Master Mathematical Constants and Rounding
Mathematical Constants
Applying Mathematical Constants in a Temperature Control System
Improved Rounding Techniques
Applying Improved Rounding Techniques
Summary
Index
Epilogue
Chapter 1: Potential of Variadic Power in C++23
Overview
In this chapter, we will look at the enormous potential of variadic features in C++23, specifically how they can enable your code to handle varying amounts of data in a dynamic and efficient way. To start, we will take a look at variadic templates, which are a great way to make your code more versatile since they let functions and classes accept multiple arguments. Your programming efficiency will improve as you learn how to write highly adaptable functions that can process variable data types and sizes.
A feature that simplifies operations on parameter packs is fold expressions, which we will explore next. By learning how to use fold expressions, you can simplify your code and remove the need for loop-like structures or manual recursion when dealing with large sets of arguments. We then move on to mastering std::tuple and std::variant, two