Home > Software engineering >  Best way to parallize this for loop with multiple threads
Best way to parallize this for loop with multiple threads

Time:03-19

I currently have a code block like this

UINT8* u = getResult();
for (UINT64 counter = 0; counter < MaxCount; counter  )
{
    for (UINT64 index = 0; index < c_uOneMB;   index)
    {
        *u   = genValue();
    }
}

Now in order to make this run faster. I am doing something like this. Basically splitting the inner thread into a method. However I have two concerns which I am not sure how to tackle.

  1. *u how do I handle that?
  2. Before calling doSomethingElse() all the threads need to .join()

Any suggestions on how to accomplish that?

void doSomething(UINT8* u)
{
      for (UINT64 index = 0; index < c_uOneMB;   index)
      {
          *u   = genValue();
      }
}

UINT8* u = getResult();
for (UINT64 counter = 0; counter < MaxCount; counter  )
{
    std::thread t(doSomething,u);
}

doSomethingElse();

CodePudding user response:

With little details you have provided I can give only this:

std::generate_n(std::execution::par, getResult(), MaxCount * c_uOneMB, genValue);

CodePudding user response:

Best way to parallize this for loop with multiple threads

Best way depends on many factors and is subjective. In fact, sometimes (perhaps most of the time) non-parallelised code is faster. If speed is most important, then the best way is whatever you have measured to be fastest.

Using the standard library algorithms is usually straightforward:

std::generate_n(
    std::execution::par_unseq,
    u,
    MaxCount * c_uOneMB,
    genValue);
  • Related