Commit 30dfab42 authored by Branislav Katreniak's avatar Branislav Katreniak Committed by Christian Hitz
Browse files

ds/random: fix threaded usage by switching to boost::random

The random threading unit test was failing on dss11-1gb oe-core
plarform with std random:

````
-------------------------------------------------------------------------------
dsRandomRandint each thread is randomly seeded
-------------------------------------------------------------------------------
../src/ds/random-test.cpp:54
...............................................................................

../src/ds/random-test.cpp:54: FAILED:
due to a fatal error condition:
  SIGINT - Terminal interrupt signal

````

Test passes on dss11-1gb oe-core plarform with boost random.

The used boost::random methods are header only,
no need to link boost::random
No related merge requests found
Showing with 8 additions and 6 deletions
+8 -6
......@@ -24,12 +24,12 @@
namespace ds {
namespace detail {
std::default_random_engine& getRandomEngine() {
boost::random::minstd_rand& threadLocalRandomEngine() {
// TODO(c++11): use thread_local std::unique_ptr
static boost::thread_specific_ptr<std::default_random_engine> engine;
static boost::thread_specific_ptr<boost::random::minstd_rand> engine;
if (!engine.get()) {
std::random_device device;
engine.reset(new std::default_random_engine(device()));
engine.reset(new boost::random::minstd_rand(device()));
}
return *engine;
}
......
......@@ -18,20 +18,22 @@
*/
#pragma once
#include <random>
#include <boost/random.hpp>
namespace ds {
namespace detail {
/// Get thread local random engine initially seeded to random value.
std::default_random_engine& getRandomEngine();
///
/// BUG: `std::default_random_engine` was failing with threads on oe-core platform
boost::random::minstd_rand& threadLocalRandomEngine();
} // detail
/// Generates a random integer in the closed interval [a, b].
/// Poor man's std::experimental::randint
template <class IntType>
IntType randint(IntType a, IntType b) {
return std::uniform_int_distribution<IntType>(a, b)(detail::getRandomEngine());
return boost::uniform_int<IntType>(a, b)(detail::threadLocalRandomEngine());
}
} // namespace ds
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment