Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Best way to parallize this for loop with multiple threads

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading