IoT Development with POCO C++ libraries and macchinaio

Recently Günter Obiltschnig from the POCO Project gave a talk about IoT Development with POCO C++ libraries and macchinaio at a by macchina.io sponsored Meetup of Meeting C++ online.

IoT Development with POCO C++ libraries and macchinaio

by Günter Obiltschnig

Chapter Videos:

POCO C++ Libraries overview

Macchina.io overview

Projects using POCO C++ Libaries and macchina.io in the real world

Q&A with Günter Obiltschnig after the talk

std::array in C++ isn't slower than array in C

In my previous article on arrays, some readers expressed concern that std::array might be slower than the built-in C array. Several sources of truth exist on this matter, and today we'll go through each of them. Let's first find out what the standard states about it, then look at the std::array implementations in libc++ and libstdc++, and finally look at the assembler of some operations on these objects. Oh, and we'll top it off with benchmarking, of course.

std::array in C++ isn't slower than array in C

by Anton Tretyakov

From the article:

Let's get to the bottom of this. LLVM has a hardening mechanism called _LIBCPP_HARDENING_MODE. We can use it to enable additional checks depending on the mechanism level, which has a total of four levels. Enabling the weakest one removes the checks from the code. In other cases, there may or may not be a check, depending on the check and the level of the mode. We'll prove it. To understand what expands to what, we need to look at the source code. There, we see that depending on the given value of _LIBCPPP_HARDENING_MODE, _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS may expand to _LIBCPPP_ASSERT.

In an Atomic World -- Lucian Radu Teodorescu

logo.pngAtomics form a relatively low level, but fundamental part of sharing data across threads. Lucian Radu Teodorescu reminds us what atomics are and how and when to use them.

In an Atomic World

by Lucian Radu Teodorescu

From the article:

We often discuss mutexes as the basic building blocks of concurrency. However, there are more fundamental concepts upon which concurrent programs and synchronization primitives are constructed. The C++ language defines a memory model, which describes how programs behave when multiple threads are involved. Additionally, C++ introduces atomic operations that serve as foundation for working with data across threads, ensuring both safety and performance. The goal of C++ atomics is to closely align with the hardware and eliminate the need for lower-level operations that must work across threads.

The topic of atomics is often overlooked, and the prevailing advice is to avoid them. While this advice is generally sound, there are occasions when we need to use atomics to fully leverage the language’s capabilities. This article aims to give atomics the attention they deserve, as they have yet to be featured in an Overload article.

The subject of atomics is extensive. For a comprehensive exploration, readers are encouraged to consult books by Anthony Williams [Williams19] and Mara Bos [Bos23]. While the Bos book primarily focuses on Rust, there is still much to be learned about atomics for C++ programmers. The reader can also consider cppreference.com for a quick reference to the atomics library [cppreference-1] In this article, we will examine various memory ordering models and illustrate their usage through simplified practical examples.

C++ programmer's guide to undefined behavior: part 6 of 11

Your attention is invited to the sixth part of an e-book on undefined behavior. This is not a textbook, as it's intended for those who are already familiar with C++ programming. It's a kind of C++ programmer's guide to undefined behavior and to its most secret and exotic corners. The book was written by Dmitry Sviridkin and edited by Andrey Karpov.

C++ programmer's guide to undefined behavior: part 6 of 11

by Dmitry Sviridkin

From the article:

I/O streams have other flags that represent the state of the stream: whether there were errors, whether we reached the end. Many people know that you can check whether an operation was successful by putting a stream object into a conditional statement (or any context where it is converted to bool). Those unfamiliar with it might use the while (!iss.eof()) check that will one day lead to the infinite loop issue. This happens when the file isn't finished, but can no longer be read—say, if the file is on a network drive, and the network has gone down. Well, that's a story for another time. Let's focus on the correct way to check readability.

Meeting C++ 2024: the online track is complete

The last part of the program for Meeting C++ 2024 is now ready: the online track.

The online track for Meeting C++ 2024 is complete!

by Jens Weller

From the article:

With this the program for Meeting C++ 2024 is now complete! The online track features 11 talks and will be prerecorded publically in October.

The talks of the online track are:

Temporarily Dropping a Lock: The Anti-lock Pattern -- Raymond Chen

RaymondChen_5in-150x150.jpgIn C++, it's common to use RAII types like std::lock_guard to manage synchronization primitives, ensuring a lock is acquired at object creation and released at destruction. However, a less common but useful pattern is the "anti-lock," which temporarily releases a lock and reacquires it later, useful in scenarios where you need to drop a lock while performing certain operations, like calling out to other components to avoid deadlocks.

Temporarily Dropping a Lock: The Anti-lock Pattern

by Raymond Chen

From the article:

There is a common pattern in C++ of using an RAII type to manage a synchronization primitive. There are different versions of this, but they all have the same basic pattern:

  • Creating the object from a synchronization object: Locks the synchronization object.
  • Destructing the object: Unlocks the synchronization object.

These types go by various names, like std::lock_guardstd::unique_lock, or std::coped_lock, and specific libraries may have versions for their own types, such as C++/WinRT’s winrt::slim_lock_guard and WIL’s wil::rwlock_release_exclusive_scope_exit (which you thankfully never actually write out; just use auto).

One thing that is missing from most standard libraries, however, is the anti-lock.

The idea of the anti-lock is that it counteracts an active lock.

CopperSpice: Template Design With Policy Classes

New video on the CopperSpice YouTube Channel:

Template Design With Policy Classes

by Barbara Geller and Ansel Sermersheim

About the video:

We have a new C++ video which discusses Policy Based Design and compares it to other styles of programming. Do you know which design pattern policy based programming solves? Have you considered the benefits of a design which provides a solution at compile time versus run time? Are you using policies and maybe you had no idea they had a name?

Please take a look and remember to subscribe.

Reflection-based JSON in C++ at Gigabytes per Second -- Daniel Lemire

portrait2018.jpgJSON is a widely-used format for data exchange, but in C++, handling JSON efficiently can be challenging. While current solutions like simdjson offer high-speed processing, upcoming features in C++26, such as powerful reflection, promise to simplify and accelerate the serialization and deserialization of JSON, making it both faster and more convenient for developers.

Reflection-based JSON in C++ at Gigabytes per Second

by Daniel Lemire

From the article:

JSON (JavaScript Object Notation) is a popular format for storing and transmitting data. It uses human-readable text to represent structured data in the form of attribute–value pairs and arrays. E.g., {"age":5, "name":"Daniel", toys:["wooden dog", "little car"]}. Ingesting and producing JSON documents can be a performance bottleneck. Thankfully, a few JSON parsers such as simdjson have shown that we can process JSON at high speeds, reaching gigabytes per second.

However, producing and ingesting JSON data can remain a chore in C++. The programmer often needs to address potential errors such as unexpected content.

Yet, often, the programmer only needs to map the content to and from a native C/C++ data structure.

User-Defined Formatting in std::format – Part 3 -- Spencer Collyer

logo.pngWe’ve seen formatting for simple classes and more complicated types. Spencer Collyer finishes his series by showing us how to apply specific formatting to existing classes.

User-Defined Formatting in std::format – Part 3

by Spencer Collyer

From the article:

In the previous articles in this series [Collyer24a], [Collyer24b] I showed how to write classes to format user-defined classes and container classes using the std::format library.

In this article I will show you how to create format wrappers, special purpose classes that allow you to apply specific formatting to objects of existing classes.

A note on the code listings: The code listings in this article have lines labelled with comments like // 1. Where these lines are referred to in the text of this article it will be as ‘line 1’ for instance, rather than ‘the line labelled // 1’.

Format wrappers

I’d now like to introduce a type of class which I call ‘format wrappers’. A format wrapper is a very simple class which wraps a value of another type. They exist purely so that we can define a formatter for the format wrapper. The idea is that the formatter will then output the wrapped value using a specific set of formatting rules. Hopefully this will become clearer when we discuss the Quoted format wrapper later.

A format wrapper is a very simple class, which normally consists of just a constructor taking an object of the wrapped type, and a public member variable holding a copy or reference to that value. They are intended to be used in the argument list of one of std::format’s formatting functions, purely as a way to select the correct formatter.

Adding trainings to Meeting C++ 2024

Meeting C++ now offers 4 trainings that align with Meeting C++ 2024, and will be held in the last week of November.

Adding C++ trainings to Meeting Cpp 2024

by Jens Weller

From the article:

Trainings listing

    C++ for C Developers - Migrating from C to C++ - a two day training by Slobodan Dmitrovic starting November 25th
    Program with GUTs - a half day training by Kevlin Henney on November 25th
    Generic programming in C++ with templates and auto - full day training by Nicolai Josuttis on November 28th
    Concepts, Ranges, and Views - The New Way of Programming in C++ - full day training by Nicolai Josuttis on November 29th

These 4 trainings focus on various important current aspects of C++. From the migration to C++ from C, which is also a great course if you migrate to Modern C++ from "C with classes" like code. Or a refresher on unit testing by Kevlin Henney himself. Nicolai Josuttis gives a two day training, which is also available as single days: on the first day generic programming with templates and auto is bringing you a referesher, while focusing on C++20 Concepts, Ranges and Views on the next day. I've made the decision that prices for trainings are now fixed, half/full day trainings are 499 € and two day trainings are 999 €, this already includes taxes and all fees from the ticketshop. Attending the trainings will let you learn great new ways to think about your code and it gives support Meeting C++!