diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h --- a/src/test/test_bitcoin.h +++ b/src/test/test_bitcoin.h @@ -22,7 +22,14 @@ */ #define NULLPTR(T) static_cast(nullptr) -thread_local extern FastRandomContext g_insecure_rand_ctx; +/** + * This global and the helpers that use it are not thread-safe. + * + * If thread-safety is needed, the global could be made thread_local (given + * that thread_local is supported on all architectures we support) or a + * per-thread instance could be used in the multi-threaded test. + */ +extern FastRandomContext g_insecure_rand_ctx; static inline void SeedInsecureRand(bool deterministic = false) { g_insecure_rand_ctx = FastRandomContext(deterministic); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -29,6 +29,8 @@ #include +FastRandomContext g_insecure_rand_ctx; + void CConnmanTest::AddNode(CNode &node) { LOCK(g_connman->cs_vNodes); g_connman->vNodes.push_back(&node); @@ -39,8 +41,6 @@ g_connman->vNodes.clear(); } -thread_local FastRandomContext g_insecure_rand_ctx; - std::ostream &operator<<(std::ostream &os, const uint256 &num) { os << num.ToString(); return os; diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp --- a/src/test/validation_block_tests.cpp +++ b/src/test/validation_block_tests.cpp @@ -169,12 +169,13 @@ // at random this will create parallelism and randomness inside validation - // the ValidationInterface will subscribe to events generated during block // validation and assert on ordering invariance - boost::thread_group threads; + std::vector threads; for (int i = 0; i < 10; i++) { - threads.create_thread([&config, &blocks]() { + threads.emplace_back([&config, &blocks]() { bool tlignored; + FastRandomContext insecure; for (int j = 0; j < 1000; j++) { - auto block = blocks[InsecureRandRange(blocks.size() - 1)]; + auto block = blocks[insecure.randrange(blocks.size() - 1)]; ProcessNewBlock(config, block, true, &tlignored); } @@ -190,7 +191,9 @@ }); } - threads.join_all(); + for (auto &t : threads) { + t.join(); + } while (GetMainSignals().CallbacksPending() > 0) { MilliSleep(100); }