Archive for August, 2013

C++: Polymorphic cloning and the CRTP (Curiously Recurring Template Pattern)

August 22, 2013 26 comments

A common problem in C++ occurs when you have an object of an unknown derived type and want to make a copy of it. Consider the following code:

class Vehicle {}

class Car : public Vehicle {}
class Plane : public Vehicle {}
class Train : public Vehicle {}

function make_a_copy(Vehicle *v)
  Vehicle *copy_of_vehicle = new ????(*v);

You can’t just use new Vehicle in this situation because only the base class elements (those from the Vehicle base class) will be available in the copy. So what do we do? Read more…


C++11: Multi-core Programming – PPL Parallel Aggregation Explained

August 17, 2013 6 comments

In the first part of this series we looked at general multi-threading and multi-core programming concepts without getting into the meat of any real problems, and in the second part we looked at the theory and application of the parallel aggregation pattern using either C++11’s standard thread library or Boost using classical parallel programming techniques. In this part we shall solve exactly the same summation problem from part 2 (adding all the numbers from 0 to 1 billion) but use the new Parallel Patterns Library (PPL) to do so. We will first look at a direct port using classical parallel programming techniques, then find out how to leverage new language features in C++11 which are employed by PPL to simplify the code and write parallel algorithms using an entirely new paradigm. We shall also look at some fundamental differences between PPL and other multi-threading/multi-core libraries, the pros and cons, and some gotchas to be aware of. Read more…

C++11 / Boost: Multi-threading – The Parallel Aggregation Pattern

August 15, 2013 7 comments

In the first part of this series we looked at general multi-threading and multi-core programming concepts without getting into the meat of any real problems. Tutorials on how to spin up worker threads in C(++) using POSIX/Pthreads, Windows or Boost.Thread are a dime a dozen so I won’t spend too much time on that here; instead I’ll look at a much less-documented and more complicated real-world multi-threading problem, namely that of parallel aggregation, and how to implement it both using new C++11 standard library functions, and with Boost.Thread for those who don’t have access to C++11 at present.

In the example developed in this article, we shall learn how to add all of the numbers from 0 to 1,000,000,000 (1 US billion) exclusive, using multi-threading on a multi-core system to speed up execution and make all the available cores work for us, rather than just one.

(do note that this particular problem can be solved very simply with the sum of a series formula: S = n(a1 + an) / 2 where S is the sum of the series, ax is the x‘th term in the series and n is the number of terms in the series; we are using the brute-force approach here purely for illustration)

Read more…

Crash Course: Excel VBA Macros For People Who Don’t Care About Excel VBA Macros

August 7, 2013 2 comments

Every 6-9 months I have to write some macros in Excel. I spend most of my coding time working in C++, C# and PHP and I tend to forget both VBA syntax and the Excel API really easily. So for those of you who believe – like me – that coding in VBA is anathema, I’ve thrown together a quick crash course in how to get some common tasks done with no fuss. All the examples in this article deal with writing formula macros and I’ll assume you know how to use Excel’s interface and are familiar with using formulas. In the latter part of the article, I give a few code examples for performing common tasks required by Excel macros. Read more…

%d bloggers like this: