Is opencv::flann:Index knnsearch thread safe?

时间:2016-08-31 18:53:57

标签: c++ multithreading opencv openmp

I am trying to parallelize my ML problem with flann Index. My code in nutshell looks like this:

Index index(dict, KDTreeIndexParams(TREE_NUM)); // HUGE dict, very expensive to construct -- prefer to create it once.
#pragma omp parallel for
for (int i = 0; i < featuresize; i++) {
    // creating thread-local params
    auto denseTF = index.knnSearch(<thread-local params>);
    // not relevant code
}

I looked up documentation here but there is nothing about thread safety there. My concern is whether this snippet is thread safe?

1 个答案:

答案 0 :(得分:3)

经过一天的调试和捕获数据竞争并阅读源代码(here)后,我可以得出结论index.knnSearch不是线程安全的。

在内部,indexTree正在knnSearch来电更新。我通过为每个线程创建index的副本来解决这个问题(是的,它很昂贵,但仍然比顺序代码更快)。