Let's say that in enclosing scope, I have some variables that each thread in a parallel_for loop should access. I have an idea combinable would suits, making one copy of my variable in each thread. However, I don't understand how to initialize my combinable thing:

For instance I want to use copies of a QString formula. I create a combinable standing for the copied things in each thread:

 combinable<QString> formulaRx;

Should I write then :

parallel_for(0,p,[&formulaRx, formula](int i)
{
         formulaRx.local() = formula;

         // do things

 });

? Would this solve the problem, or would there still be concurrent access with the line

 formulaRx.local() = formula;

What is the proper way to handle this?

Please tell me if not clear

Thanks and regardS.

有帮助吗?

解决方案

you do not need to assign formulaRx.local(). This is a getter for the local copy of the combinable object.

parallel_for(0,p,(int i)
{
     QString f = formulaRx.local();

     // do things with f which is the local copy of formulaRx

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