Using a condition variable in C++

A condition variable (std::condition_variable from the <condition_variable> header) is an abstraction allowing for one or multiple threads to wait on an event associated with modification of some shared variable. As an example, consider two threads, denoted the publisher and the subscriber. The former generates a series of events, with some associated data. The latter subscribes to these events and processes the data elements as they become available. For making this possible, we will use the global mutex and condition variable:

std::vector and C++'s braced initialization

The new versions of C++ (starting from C++11) include a unified initialization syntax based on braces. In most of the cases it offers a more elegant and unified way of initializing objects. Examples of its usage are as follows: std::string name{"Michael Scott"}; MyClass obj{}; // default constructor Braced initialization can also be used in constructors to initialize class members: MyClass::MyClass(const Something& value) : member{value} { // Additional operations ... } When it comes to be widely-used std::vector, braced initialization might be a source of confusion.

Serve images on the fly in a binary form with Flask

In this post I am going to demonstrate how to serve images from a REST endpoint for a situation when the images are not stored as static files, but generated on the fly given some original form. This use case will be described with application of Flask, OpenCV and NumPy. A situation when this functionality is needed is when the original images are too large, and their downscaled representations are sometimes required, or when the original format of the images is too heavy to be served via a web service (like BMP).

Searching for the closest value in a sorted vector with C++

In this post I am going to show how to efficiently search for the closest element in sorted std::vector using the std::lower_bound function from the Standard Template Library (STL). The problem is a follows: you have a bunch of numbers (e.g. doubles) that you have stored in a std::vector, which you have subsequently sorted, and you want to, given a new number x, find the element in the sorted vector that is the closest to x.

Building C++ code from command line on Windows

After many years of working exclusively in Unix environments, Windows experience appears daunting, in particular when it comes doing stuff in terminal. I have heard a lot of about the power of PowerShell, but I have never had enough motivation to explore it — I sticked to the familar Bash on Mac and Linux for most of my work. However, I was always interested in mastering the task of building C++ code using the native compiler on Windows (Visual C++) without using any GUI tools.

Optimization with Python

Mathematical optimization is a pervasive task in numerous engineering applications. It is concerned with algorithmic search for a value (usually a vector) that minimizes or maximizes a predefined objective function. In machine learning (ML), for instance, a search for the best configuration of model parameters is performed with an optimization algorithm minimizing the objective function that measures misfit between the known response values and those predicted by the ML model.

Applying a command to each line in a file with xargs -L1

Let’s say you have a text file which you wish to use an input to some command-line tool. Specifically, the goal is to apply the same command to each line in the file. This short post showcases how this can be done in a Unix shell (such as Bash) using a combination of cat, xargs and piping. As a motivating example, consider a file repos.txt containing URLs of several Github repositories, and our goal is to clone each of the repositiries.

Position independent code

In this blog post, I am going to provide a short overview of the concept of position independent code and its specification in CMake, particularly in projects using pybind11. Some time ago, when I was working on the FxIS project, which was developed in C++ with the additional Python extension realized with pybind11, I encountered a linking error, which included a line similar to this: /usr/bin/ld: libcore.a(sum.cpp.o): relocation R_X86_64_PC32 against symbol .

C++ move semantics

In the modern versions of C++ (starting from C++11) one encounters a concept of move semantics. It may confusing a bit, and in this post I am going to provide my notes on the topic (mostly based on Bjarne Stroustrup’s The C++ Programming Language (4th edition): sections 3.3, 7.7.2, 17.5.2). The first important concept is rvalue reference. For a type T, a rvalue reference is T&& (note the double ampresand). It basically represents a reference to a temporary object that is assumed to be modified and not used again.

Cancelling bad stuff in Git

In this post I am going to provide some examples of Git usage that may come in handy in case of unsuccessful previous actions during staging, committing, and merging. Let’s say you have staged some files to be further committed with git add and then change your mind. To unstage all or some files, invoke the git reset command (which is in a way an inverse of git add). A scenario for all changed files: