in src/annoylib.h [1537:1562]
static void build(AnnoyIndex<S, T, D, Random, AnnoyIndexMultiThreadedBuildPolicy>* annoy, int q, int n_threads) {
AnnoyIndexMultiThreadedBuildPolicy threaded_build_policy;
if (n_threads == -1) {
// If the hardware_concurrency() value is not well defined or not computable, it returns 0.
// We guard against this by using at least 1 thread.
n_threads = std::max(1, (int)std::thread::hardware_concurrency());
}
vector<std::thread> threads(n_threads);
for (int thread_idx = 0; thread_idx < n_threads; thread_idx++) {
int trees_per_thread = q == -1 ? -1 : (int)floor((q + thread_idx) / n_threads);
threads[thread_idx] = std::thread(
&AnnoyIndex<S, T, D, Random, AnnoyIndexMultiThreadedBuildPolicy>::thread_build,
annoy,
trees_per_thread,
thread_idx,
std::ref(threaded_build_policy)
);
}
for (auto& thread : threads) {
thread.join();
}
}