atomic_thread_fence

Just like you can have a std::atomic synchronizes two threads with each other with release-acquire semantic, you can also have Fence-Fence, Atomic-Fence, Fence-Atomic synchronizations. C++ reference has very detailed documentation about when there exists a valid synchronizes-with [https://preshing.com/20130823/the-synchronizes-with-relation/] relationship, https://en.cppreference.com/w/cpp/…

How C++ `typeid` operator works

C++ language include an operator called typeid ( https://en.cppreference.com/w/cpp/language/typeid). > Queries information of a type. Used where the dynamic type of a polymorphic object must be known and for static type identification. It gives you information about the type of an object, as long as…

GNU Visibility Attribute

When going through the folly [https://github.com/facebook/folly] code base, sometimes, you see definitions like class FOLLY_EXPORT OptionalEmptyException : public std::runtime_error { ... FOLLY_EXPORT is defined as #define FOLLY_EXPORT __attribute__((__visibility__("default"))) It sets the visibility attribute of symbol OptionalEmptyException to "default". What does it do…

std::atomic from bottom up

std::atomic combines read-modify-write atomic instructions, memory barriers in hardware and memory order concept in C++ altogether. It's commonly used for lock-free programming, which often looks like, if (futex.compare_exchange_strong(expected, 1, memory_order_acquire)) { // lock acquired } Multiple Caches Data race doesn't exist until we had computers with…

Linear scalable read-write lock

The basic concept of a read-write lock is simple. It allows multiple readers to access the resource simultaneously, but at most one thread can have exclusive ownership of the lock (a.k.a write lock). It's supposed to be an optimization, comparing to simple mutex (e.g. std::mutex). As…

Global Data Locality – Why and How

Why In folly [https://github.com/facebook/folly], Facebook's open source c++ library, you often see code [https://github.com/facebook/folly/blob/d2c64d94c7e892925a02a080c886ab3df3f5c937/folly/experimental/settings/Settings.h#L168] like the following: #define FOLLY_SETTING_DEFINE(_project, _name, _Type, _def, _desc) \ /* Fastpath optimization, see notes in FOLLY_SETTINGS_DEFINE_…

Unary Plus

A few times I ran into the following issue uint8_t a = 0; std::cout << "a = " << a << std::endl; Guess what would it print? a = What happened here is that it printed NULL (0) in ASCII. uint8_t is just a typedef of unsigned char. typedef unsigned char uint8_t;…

Stack overflow

I recently ran into a program crash. There were some mysteries at the beginning of the debugging process. Mysteries Signal handler not called The program crashed with a coredump, according to which, it crashed with SIGSEGV. The mystery is that the code has registered a signal handler, which should get…