storage_adaptor_threaded_test.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/core/lightweight_test.hpp>
  7. #include <boost/histogram/accumulators/ostream.hpp>
  8. #include <boost/histogram/accumulators/thread_safe.hpp>
  9. #include "throw_exception.hpp"
  10. #include <boost/histogram/storage_adaptor.hpp>
  11. #include <array>
  12. #include <deque>
  13. #include <map>
  14. #include <thread>
  15. #include <unordered_map>
  16. #include <vector>
  17. using namespace boost::histogram;
  18. constexpr auto n_fill = 1000000;
  19. template <class T>
  20. void tests() {
  21. {
  22. storage_adaptor<T> s;
  23. s.reset(1);
  24. auto fill = [&s]() {
  25. for (unsigned i = 0; i < n_fill; ++i) {
  26. ++s[0];
  27. s[0] += 1;
  28. }
  29. };
  30. std::thread t1(fill);
  31. std::thread t2(fill);
  32. std::thread t3(fill);
  33. std::thread t4(fill);
  34. t1.join();
  35. t2.join();
  36. t3.join();
  37. t4.join();
  38. BOOST_TEST_EQ(s[0], 4 * 2 * n_fill);
  39. }
  40. }
  41. int main() {
  42. using ts_int = accumulators::thread_safe<int>;
  43. tests<std::vector<ts_int>>();
  44. tests<std::array<ts_int, 100>>();
  45. tests<std::deque<ts_int>>();
  46. // stdlib maps are not thread-safe and not supported
  47. return boost::report_errors();
  48. }