Articles & Books

Finite State Machine with std::variant - Vending Machine -- Bartlomiej Filipek

In my last article, we discussed Finite State Machines based on std::variant and some cool C++17 techniques. Today I want to go further and show you an example of a Vending Machine implementation.

Finite State Machine with std::variant - Vending Machine

By Bartlomiej Filipek

From the article:

Here’s a basic diagram that illustrates how the machine is going to work:

vending_states.png

Following the model from the last article, each state and the event will be a separate small structure. Later we can “pack” them into std::variant.

C++23: The Next C++ Standard -- Rainer Grimm

TimelineCpp.pngC++23 will be the next C++ standard after C++20. This new standard significantly improves C++ but is less game-changing than C++98, C++11, or C++20. C++23 is more in the tradition of C++17.

C++23: The Next C++ Standard

By Rainer Grimm

From the article:

The C++ Standards

C++ is more than 40 years old. What happened in the last years? Here is a simplified answer ending in C++23.

C++98

At the end of the 80ths, Bjarne Stroustrup and Margaret A. Ellis wrote their famous book Annotated C++ Reference Manual (ARM). These books served two purposes. First, there were many independent C++ implementations. ARM defined, therefore, the functionality of C++. Second, ARM was the base for the first C++ standard: C++98 (ISO/IEC 14882). C++98 had a few essential features: templates, the standard template library (STL) with its containers and algorithms, strings, and IO streams.

C++03

With C++03 (14882:2003), C++98 got a technical correction that is so small that there is no place on my timeline. In the community, C++03, which includes C++98, is called legacy C++.

Constrain your user-defined conversions -- Jonathan Mueller

jonathanmueller.pngSometimes you want to add an implicit conversion to a type. This can be done by adding an implicit conversion operator. For example, std::string is implicitly convertible to std::string_view.

Constrain Your User-Defined Conversions

By Jonathan Mueller

From the article:

Sometimes you want to add an implicit conversion to a type. This can be done by adding an implicit conversion operator. For example, std::string is implicitly convertible to std::string_view:

	class string { // template omitted for simplicity
	public:
	    operator std::string_view() const noexcept
	    {
	       return std::string_view(c_str(), size());
	    }
	}; 

The conversion is safe, cheap, and std::string and std::string_view represent the same platonic value — we match Tony van Eerd’s criteria for implicit conversions and using implicit conversions is justified.

However, even when all criteria are fulfilled, the conversion can still be dangerous.

Thread-Safe Queue - Two Serious Errors -- Rainer Grimm

concurrentarchitecture-grimm.pngIn my last post "Monitor Object"  I implemented a thread-safe queue. I made two serious errors. Sorry. Today, I will fix these issues.

Thread-Safe Queue - Two Serious Errors

by Rainer Grimm

From the article:

First, I want to show you again the erroneous implementation from my last post to understand the context.

// monitorObject.cpp

#include <condition_variable>
#include <functional>
#include <queue>
#include <iostream>
#include <mutex>
#include <random>
#include <thread>

class Monitor {
public:
    void lock() const {
        monitMutex.lock();
    }

    void unlock() const {
        monitMutex.unlock();
    }

    void notify_one() const noexcept {
        monitCond.notify_one();
    }

    template <typename Predicate>
    void wait(Predicate pred) const {                 // (10)
        std::unique_lock<std::mutex> monitLock(monitMutex);
        monitCond.wait(monitLock, pred);
    }
  
private:
    mutable std::mutex monitMutex;
    mutable std::condition_variable monitCond;
};

template <typename T>                                  // (1)
class ThreadSafeQueue: public Monitor {
public:
    void add(T val){
        lock();
        myQueue.push(val);                             // (6)
        unlock();
        notify_one();
    }
  
    T get(){
        wait( [this] { return ! myQueue.empty(); } );  // (2)
        lock();
        auto val = myQueue.front();                    // (4)
        myQueue.pop();                                 // (5)
        unlock();
        return val;
    }

private:
    std::queue<T> myQueue;                            // (3)
};


class Dice {
public:
    int operator()(){ return rand(); }
private:
    std::function<int()> rand = std::bind(std::uniform_int_distribution<>(1, 6),
                                          std::default_random_engine());
};


int main(){
  
    std::cout << '\n';
  
    constexpr auto NumberThreads = 100;
  
    ThreadSafeQueue<int> safeQueue;                      // (7)

    auto addLambda = [&safeQueue](int val){ safeQueue.add(val);          // (8)
                                            std::cout << val << " "
                                            << std::this_thread::get_id() << "; ";
                                          };
    auto getLambda = [&safeQueue]{ safeQueue.get(); };  // (9)

    std::vector<std::thread> addThreads(NumberThreads);
    Dice dice;
    for (auto& thr: addThreads) thr = std::thread(addLambda, dice());

    std::vector<std::thread> getThreads(NumberThreads);
    for (auto& thr: getThreads) thr = std::thread(getLambda);

    for (auto& thr: addThreads) thr.join();
    for (auto& thr: getThreads) thr.join();
  
    std::cout << "\n\n";
   
}

Object Ownership -- Ilya Doroshenko

objectownership-doroshenko.pngThis article goes over the spicy topic of object ownership. We covered the lifetime quirks, and we found out that manual memory management can be a nightmare, we new and delete in the correct order. There must be something better than that. Well, there is but it comes with its own can of worms.

Object Ownership

By Ilya Doroshenko

From the article:

Since we know the rules of new and delete, namely new allocates and delete destroys, we never really cared about who is responsible for the object. This caused a lot of confusion in the past. For instance, some API codes from Win32 return strings that should be LocalFree()d, like FormatMessage or GetEnvironmentStrings. POSIX, on the other hand, has strdup as a common example of you should free it yourself. This model is confusing because you may have a lot of return statements, before which you should always call free or delete, depending on the operation. However, we have RAII since the very beginning of C++, which adds constructors and destructors. So, in 1998 resourceful people decided to add auto_ptr to the standard.

The premise was simple:

  • a simple explicit constructor, that took raw pointer from new
  • destroyed by either an explicit release/reset or a destructor on the end of the block

This was the first attempt at a structured cleanup. As time passed, the initial point began to crumble. More issues arose and the question came up: Who owns the data?

 

 

Parsing Time Stamps Faster with SIMD Instructions -- Daniel Lemire

In software, it is common to represent time as a time-stamp string. It is usually specified by a time format string. Some standards use the format %Y%m%d%H%M%S meaning that we print the year, the month, the day, the hours, the minutes and the seconds. The current time as I write this blog post would be 20230701205436 as a time stamp in this format. It is convenient because it is short, easy to read and if you sort the strings lexicographically, you also sort them chronologically.

Parsing Time Stamps Faster with SIMD Instructions

by Daniel Lemire

From the article:

You can generate time stamps using any programming language. In C, the following program will print the current time (universal, not local time):

We are interested in the problem of parsing these strings. In practice, this means that we want to ...

 

AddressSanitizer continue_on_error -- Jim Radigan

jim-300x300.jpgVisual Studio 17.6 comes with new functionality in the Address Sanitizer runtime which provides a new “checked build” for C and C++. This new runtime mode diagnoses and reports hidden memory safety errors, with zero false positives, as your app runs.

AddressSanitizer continue_on_error

by Jim Radigan

From the article:

C++ memory safety errors are a top concern for the industry. In Visual Studio 17.6, we deliver a new experimental Address Sanitizer feature: continue_on_error (COE). We’ll remove the experimental label in 17.8. You compile as before, by simply adding the compiler flag -fsanitizer=address. With 17.6 you can enable the COE functionality by setting environment variables from the command line.

Monitor Object -- Rainer Grimm

concurrentarchitecture-grimm.pngThe Monitor Object design pattern synchronizes concurrent member function execution to ensure that only one member function runs within an object at a time. It also allows an object's member functions to schedule their execution sequences cooperatively.

Monitor Object

by Rainer Grimm

From the article:

Problem

If many threads access a shared object concurrently, the following challenges exist.

  • Due to the concurrent access, the shared object must be protected from non-synchronized read and write operations to avoid data races.
  • The necessary synchronization should be part of the implementation, not the interface.
  • When a thread is done with the shared object, a notification should be triggered so the next thread can use the shared object. This mechanism helps avoid and improves the system's overall performance.
  • After the execution of a member function, the invariants of the shared object must hold.

Solution

A client (thread) can access the Monitor Object's synchronized member functions, and due to the monitor lock, only one synchronized member function can run at any given time. Each Monitor Object has a monitor condition that notifies the waiting clients.

 

Finite State Machine with std::variant -- Bartlomiej Filipek

In this blog post, I’ll show you how to convert a “regular” enum-style finite state machine into a modern version based on std::variant from C++17. This technique allows you to improve design, work with value types and enhance code quality.

Finite State Machine with std::variant

by Bartlomiej Filipek

From the article:

Let’s start with a basic example:

  • we want to track a game player’s health status
  • we’d like to respond to events like “Hit by a monster” or “Healing bonus.”
  • when the health point goes to 0, then we have to restart the game only if there are some remaining lives available

Here’s a basic diagram with states and transitions:

New C++ Features in GCC13 -- Marek Polacek

newfeaturesc23.pngThe latest major version of the GNU Compiler Collection (GCC), 13.1, was released in April 2023. Like every major GCC release, this version brings many additions, improvements, bug fixes, and new features.

New C++ features in GCC 13

by Marek Polacek

 

From the article:

Like the article I wrote about GCC 10 and GCC 12, this article describes only new features implemented in the C++ front end; it does not discuss developments in the C++ language itself. Interesting changes in the standard C++ library that comes with GCC 13 are described in a separate blog post: New C features in GCC 13