diff options
Diffstat (limited to 'src/boost/libs/histogram/examples/guide_parallel_filling.cpp')
-rw-r--r-- | src/boost/libs/histogram/examples/guide_parallel_filling.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/boost/libs/histogram/examples/guide_parallel_filling.cpp b/src/boost/libs/histogram/examples/guide_parallel_filling.cpp new file mode 100644 index 000000000..f4a23c9c7 --- /dev/null +++ b/src/boost/libs/histogram/examples/guide_parallel_filling.cpp @@ -0,0 +1,50 @@ +// Copyright 2018-2019 Hans Dembinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) + +//[ guide_parallel_filling + +#include <boost/histogram.hpp> +#include <boost/histogram/algorithm/sum.hpp> +#include <cassert> +#include <functional> +#include <thread> +#include <vector> + +// dummy fill function, to be executed in parallel by several threads +template <typename Histogram> +void fill(Histogram& h) { + for (unsigned i = 0; i < 1000; ++i) { h(i % 10); } +} + +int main() { + using namespace boost::histogram; + + /* + Create histogram with container of thread-safe counters for parallel filling in + several threads. Only filling is thread-safe, other guarantees are not given. + */ + auto h = make_histogram_with(dense_storage<accumulators::thread_safe<unsigned>>(), + axis::integer<>(0, 10)); + + /* + Run the fill function in parallel from different threads. This is safe when a + thread-safe accumulator and a storage with thread-safe cell access are used. + */ + auto fill_h = [&h]() { fill(h); }; + std::thread t1(fill_h); + std::thread t2(fill_h); + std::thread t3(fill_h); + std::thread t4(fill_h); + t1.join(); + t2.join(); + t3.join(); + t4.join(); + + // Without a thread-safe accumulator, this number may be smaller. + assert(algorithm::sum(h) == 4000); +} + +//] |