Archive

Posts Tagged ‘chrono’

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

August 15, 2013 8 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…

Advertisements
%d bloggers like this: