Dekker’s algorithm is a way of synchronizing two threads around a critical section. It is a dance where thread 1 sets its flag declaring its intent to enter the critical section, then it checks the flag of thread 2.
Atomic Weapons
Long but worth watching. This will take your C++ to the next level 😉
Memory barriers and thread synchronization
We will jump straight to the code. This innocent looking little program has a major issue (when compiled for release build with optimizations on my Mac using GCC, Apple’s CLANG, and LLVM, as well as on Windows using Visual Studio 2017, and ran on a multicore machine). Can you spot the problem?
Atomic blocking queue
As I learn about atomics and memory model I decided to take a stab at rewriting my blocking queue using atomic operations and eliminate the mutex around the critical section of code responsible for pushing and popping elements, effectively creating a fast path
Synchronizing with atomics
I’ve been reading and learning about the C++ memory model and relaxed atomics and decided to rewrite my Interview question, part 1 using atomic
Fun with unordered containers
I’ve been playing around with unordered_map and unordered_set to see how they grow when elements are inserted. Here’s what I learned so far: the default load factor is 1; this means the container will grow to accommodate at most 1 element per bucket. You can change the load factor with max_load_factor call followed by
Short introduction to parameter packs and fold expressions
Pure virtual destructor
Did you know that a destructor can be pure virtual? It can be defined as such, but it still needs a body declaration. It is a way of making a class abstract (instances of it cannot be created) without having to make any pure virtual methods. So use it if you have a base
Read/write mutex
Designed and implemented by Chris M. Thomasson. Complete implementation can be found on GitHub.
GitHub repository
All the code from this blog is now on GitHub under MIT License: mvorbrodt/blog
Templates in STL containers
Storing template classes in STL containers is tricky 🙂 If v is a std::vector and TC is a template class, how can we do the following:
Multiple return values
C++17 gives us an elegant way to return multiple values from a function using structured binding.
Simple timer
Jonathan Boccara over at Fluent{C++} made a post a while ago titled A Simple Timer in C++. I felt
Interview question, part 6
Given a set of integer ranges defined as [LO, HI) and a value P, find which range P falls into.
My approach to this programming puzzle was to first define a range as a struct that can be sorted (thanks to operator < ), then perform binary search on a vector of sorted ranges. The code
Fast mutex
Many thanks to Chris M. Thomasson for rewriting POSIX Threads for Win32 mutex into standard C++ implementation. Using auto_event class from
Simple thread pool
I know the topic of thread pools has been beaten to death on the internet, nevertheless I wanted to present to you my implementation which uses only standard C++ components 🙂
Inverting std::map and std::multimap
A junior colleague asked me about the following problem: given a std::map or std::multimap of key to value pairs, sort it by values; or turn it into value to key mapping.In order to solve this problem we need two data structures: the source std::map or std::multimap of key to value pairs, and a second
Interview question, part 5
Find the starting position(s) of “runs” (consecutive characters) in the input from left to right.
Interview question, part 4
I dug up another one from years ago 🙂 This one asked to write a program that:
Prints all binary numbers that do not contain consecutive bits set.
Interview question, part 3
I was given this piece of code to analyze and point out what’s wrong with it: