0

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.

kiriloff
  • 22,522
  • 32
  • 127
  • 207

1 Answers1

1

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

});
Rotem
  • 19,723
  • 6
  • 57
  • 101
  • Thanks. However, I should have been more precise: I am also working with pointers in the same situation:: what if formula is a pointer? Then I guess 'pointers' f would point to same thing. – kiriloff Apr 30 '12 at 15:59