Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2018-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <util/string.h>6#include <util/threadnames.h>8#include <mutex>9#include <set>10#include <string>11#include <thread>12#include <vector>14#include <boost/test/unit_test.hpp>16using util::ToString;18BOOST_AUTO_TEST_SUITE(util_threadnames_tests)20const std::string TEST_THREAD_NAME_BASE = "test_thrd.";22/**23* Run a bunch of threads to all call util::ThreadRename.24*25* @return the set of name each thread has after attempted renaming.26*/27std::set<std::string> RenameEnMasse(int num_threads)28{29std::vector<std::thread> threads;30std::set<std::string> names;31std::mutex lock;33auto RenameThisThread = [&](int i) {34util::ThreadRename(TEST_THREAD_NAME_BASE + ToString(i));35std::lock_guard<std::mutex> guard(lock);36names.insert(util::ThreadGetInternalName());37};39threads.reserve(num_threads);40for (int i = 0; i < num_threads; ++i) {41threads.emplace_back(RenameThisThread, i);42}44for (std::thread& thread : threads) thread.join();46return names;47}49/**50* Rename a bunch of threads with the same basename (expect_multiple=true), ensuring suffixes are51* applied properly.52*/53BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded)54{55std::set<std::string> names = RenameEnMasse(100);57BOOST_CHECK_EQUAL(names.size(), 100U);59// Names "test_thrd.[n]" should exist for n = [0, 99]60for (int i = 0; i < 100; ++i) {61BOOST_CHECK(names.contains(TEST_THREAD_NAME_BASE + ToString(i)));62}64}66BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.