- Add arena allocator for zero-allocation hot paths - Add thread pool for parallel operations - Add mmap utilities for memory-mapped I/O - Implement queue_index with heap-based priority queue - Implement dataset_hash with SIMD support (SHA-NI, ARMv8) - Add runtime SIMD detection for cross-platform correctness - Add comprehensive tests and benchmarks
49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#include "thread_pool.h"
|
|
|
|
ThreadPool::ThreadPool(size_t num_threads) {
|
|
for (size_t i = 0; i < num_threads; ++i) {
|
|
workers_.emplace_back([this] {
|
|
for (;;) {
|
|
std::function<void()> task;
|
|
{
|
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
|
condition_.wait(lock, [this] { return stop_ || !tasks_.empty(); });
|
|
if (stop_ && tasks_.empty()) return;
|
|
task = std::move(tasks_.front());
|
|
tasks_.pop();
|
|
}
|
|
task();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
ThreadPool::~ThreadPool() {
|
|
{
|
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
|
stop_ = true;
|
|
}
|
|
condition_.notify_all();
|
|
for (auto& worker : workers_) {
|
|
worker.join();
|
|
}
|
|
}
|
|
|
|
void ThreadPool::enqueue(std::function<void()> task) {
|
|
{
|
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
|
tasks_.emplace(std::move(task));
|
|
}
|
|
condition_.notify_one();
|
|
}
|
|
|
|
void ThreadPool::wait_all() {
|
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
|
condition_.wait(lock, [this] { return tasks_.empty(); });
|
|
}
|
|
|
|
uint32_t ThreadPool::default_thread_count() {
|
|
uint32_t n = std::thread::hardware_concurrency();
|
|
if (n == 0) n = 4;
|
|
return n > 8 ? 8 : n;
|
|
}
|