On windows we have the .dll files, .so files on Linux, and .dylib files on Mac. They all have one thing in common: they can be loaded at runtime and provide entry points to call into. One example is an online chat client that uses plugins to add support for more protocols (Google Chat, ICQ, […]
Micro-benchmarks
I was looking for a way to benchmark a piece of code… I came up with 5 libraries that make it very easy 🙂 I’m not going to write a tutorial on how to use each one because I would basically be rewriting their documentation sections. What I will do is show you how to […]
SQL database access
When I first set out to make a post about database access from C++ I was going to write about MySQL Connector/C++. But for some reason that didn’t sit well with me. After sleeping on it I realized it didn’t appeal to me because it was 1) limited to only one database backend and 2) […]
Measuring CPU time
Measuring how long your program ran for is easy with std::chrono, but what if you need details about user and system space time? Easy! Use Boost CPU Timer library 🙂 It’s another simple library from Boost with only few classes: auto_cpu_timer is like a RAII object; it starts the clock in its constructor, stops it […]
Printing stack traces
This one will be short 🙂 If you want to add more diagnostic information to your programs make sure to check out Boost.Stacktrace library. With it you can capture and print current stack traces. It’s especially useful when combined with exception handling; it allows you to know right away where the exception originated from.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <future> #define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED #include <boost/stacktrace.hpp> using namespace std; using namespace boost::stacktrace; void f1(); void f2(); void f3(); void f1() { f2(); } void f2() { f3(); } void f3() { cout << stacktrace() << endl << endl; } int main(int argc, char** argv) { f1(); auto f = async(launch::async, []() { cout << stacktrace() << endl << endl; }); f.get(); return 1; } |
[…]
Generating Unique IDs
I like simple and well written C++ libraries, and I’m especially fond of Boost, so I’m going to blog about it some more 🙂 Today I’ll show you how to generate unique IDs using Boost::UUID header only library. There really isn’t much to be said about it other than it’s simple, consists of only few […]
RPC with Protocol Buffers
Just when I thought I covered the topic of RPC in C++ with Thrift, I came across a new and very promising RPC framework: Google’s gRPC. The more I read about it the more I like it: just like Thrift it supports many programing languages and internally uses Protocol Buffers to encode messages. So I […]
Serialize data to XML
Protocol Buffers are not always an option and you just have to serialize your data to XML. Then what? Luckily there is a serialization library available from Boost and it makes that pretty easy for us. You don’t even have to modify your existing data structures to write them out as XML: the library is […]
Thrift: or how to RPC
Just like my previous Protocol Buffers post, this one is also meant as a brief introduction that will point you in the right direction rather than an exhaustive tutorial. Here we go… Again we are in search of a portable library, this time not for serialization, but for a portable RPC mechanism. On Windows we […]
Protocol Buffers: or how to serialize data
This post is meant as a brief introduction that will point you in the right direction rather than an exhaustive tutorial. Here we go… Have you ever had to write code that serialized structured data into an efficient binary format, to be later saved to disk or sent over the network? Do you remember how […]
Well, that was no fun :(
No, not blogging, that’s still fun 🙂 moving my website to bluehost over the last 24 hours! But it’s finally done and the Vorbrodt’s C++ Blog is smooth sailing once again… on a brand spanking new domain! But have no fear, the old one will kindly redirect. So what have I learned through this exercise? […]
Parallel STL
C++17 standard introduced execution policies to the standard algorithms; those allow for parallel and SIMD optimizations. I wanted to see how much faster the Parallel STL can be on my quad core system but
Advanced thread pool
Below is my implementation of the thread pool described in this talk and a benchmark comparing it against my simple thread pool implementation. The advanced pool is 15x faster at scheduling
Better Code: Concurrency
by Sean Parent
Better timer class
It bothered me that my previous simple timer implementation fired off a new thread for each timeout and interval. I knew things could be done better, but didn’t yet know how. Well this morning inspiration came and I implemented new and shiny
Random number generator
Examples based on this talk.
Below is the old and rusty way of generating random numbers. Don’t do it!
rand() Considered Harmful
Hilarious and informative talk on C and C++ random number generation:
Relaxed atomics and data races
The following code is a race condition:
Bit fields and race conditions
The following program, despite looking correct, is a race condition and has unpredictable behaviour:
Dekker’s algorithm for N threads
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?