Overhead of creating lambda for multiple threads in C ++

I have concurrency::parallel_for, inside which I call a function that takes a functor, passing it a lambda, for example

concurrency::parallel_for(0, particleCount, [&](int i)
{
...
  kdTree->VisitCells([&](const KDTreeNode &node) //defining and passing
  {
   //code dealing with tree traversal
  }
}

... and the question is, what bad practice is to define a lambda inside parallel_forthat runs for each of the many particles. Will this essentially define one functor for each particle? An alternative is to write a lambda outside parallel_forand design it to make a few more arguments (since it can no longer perform the same captures as before) and pass this to the tree inside parallel_for.

When testing, it seems that it detects it a little faster parallel_for, but the data is inconclusive, and I don’t know exactly how several identical lambdas with different captured data are really processed.

+3
source share
1 answer

In general, it depends on two things.

-, "" parallel_for , - parallel_for, . [&] - . , / ( , ) , , .

-, , , , .

, , , (, , ).

, ( ), , , " parallel_for", , struct definition , ( ). , .

.... , . .

0

Source: https://habr.com/ru/post/1611124/


All Articles