my program with PPL crashes. I do suspect some mishandled share of variables. If my syntax for parallel_for construct is

parallel_for(0,p,[&x1Pt,&x2Pt,&confciInput,&formula,&param,&method,&lowOneParam,&highOneParam](int i)
{   

                 // ...

      }

, do each thread have its own copy of confciInput and formula, for example, to work with? Or does the capture clause of lambda expression only provides access to enclosing scope local variables?

Thanks and regards.

有帮助吗?

解决方案

When you capture a variable by reference in the lambda expression's capture list each thread will operate on the same variable that was captured, and this variable's value is modified in the caller's context. If you need each thread to have its own copy modify the call to

parallel_for(0,p,
  [&x1Pt,&x2Pt,confciInput,formula,&param,&method,&lowOneParam,&highOneParam]
  (int i) mutable
{   
  // ...
} );

Now, each thread has its own copy of the confciInput and formula variables, but any modifications that these threads may make to these local copies will not be made to the original variables.

Also, by default a lambda captures variables by const value, so if you're going to modify either one of variables within the lamda, you'll need the mutable specification.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top