Examples based on this talk.
Below is the old and rusty way of generating random numbers. Don’t do it!
Examples based on this talk.
Below is the old and rusty way of generating random numbers. Don’t do it!
Hilarious and informative talk on C and C++ random number generation:
The following code is a race condition:
The following program, despite looking correct, is a race condition and has unpredictable behaviour:
In the previous post I described Dekker’s algorithm. Its limitation is that it only works for 2 threads. I wanted it to work for N threads, but I can’t atomically check N-1 flags belonging to other threads 🙁 now what?
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.
Long but worth watching. This will take your C++ to the next level 😉
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?
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
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
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
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
Designed and implemented by Chris M. Thomasson. Complete implementation can be found on GitHub.
All the code from this blog is now on GitHub under MIT License: mvorbrodt/blog
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:
C++17 gives us an elegant way to return multiple values from a function using structured binding.
Jonathan Boccara over at Fluent{C++} made a post a while ago titled A Simple Timer in C++. I felt
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
Many thanks to Chris M. Thomasson for rewriting POSIX Threads for Win32 mutex into standard C++ implementation. Using auto_event class from