threadpool_wrapper.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <array>
9 #include <atomic>
10 #include <cstdio>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 
15 #include <BS_thread_pool.hpp>
16 
17 #include <pthread.h>
18 
19 namespace kvikio {
20 
24 using ThreadPool = BS::thread_pool;
25 
46 [[nodiscard]] inline std::function<void()> make_thread_pool_init_task(std::string prefix)
47 {
48  auto counter = std::make_shared<std::atomic<unsigned int>>(0);
49  return [counter = std::move(counter), prefix = std::move(prefix)]() {
50  unsigned int const idx = counter->fetch_add(1, std::memory_order_relaxed);
51  // Linux comm limit is 15 chars + NUL.
52  std::array<char, 16> name{};
53  std::snprintf(name.data(), name.size(), "%s-%u", prefix.c_str(), idx);
54  pthread_setname_np(pthread_self(), name.data());
55  };
56 }
57 
58 } // namespace kvikio
KvikIO namespace.
Definition: batch.hpp:16
BS::thread_pool ThreadPool
Thread pool type used for parallel I/O operations.
std::function< void()> make_thread_pool_init_task(std::string prefix)
Build a BS::thread_pool init task that names each worker thread.