How can I refactor this code into multi-thread version?

SimonCqk

There is a loop which takes quite a long time and I'm considering refactoring this code into multi-thread version. And here is the model.

   Photon photon;
    for (int i=0;i<1000000;++i){
         func(){
          photon.lanuch(args...){
          // do something  
          }
      }
    }

I have to call this function a thousand and thousand times.So I was wondering how can I create some threads to run this function at the some time. But the photon have to be individual every single time. the index i can be converted to this:

 atomic<int> i{0};
    while(i<1000000){
          func(){
              photon.lanuch(args...){
              // do something  
             ++i;
               }
          }
    }
Yakk - Adam Nevraumont

With threading you have to pay attention to object lifetime and sharing far more than normal.

But the basic solution is

void do_tasks( std::size_t count, std::function<void( std::size_t start, std::size_t finish )> task ) {
  auto thread_count = std::thread::hardware_concurrency();
  if (thread_count <= 0) thread_count = 1;

  std::vector<std::future<void>> threads( thread_count-1 );

  auto get_task = [=](std::size_t index) {
    auto start = count * index / thread_count;
    auto finish = count * (index+1) / thread_count;
    // std::cout << "from " << start << " to " << finish << "\n";
    return [task, start, finish]{ task(start, finish); };
  };
  for( auto& thread : threads ) {
    auto index = &thread-threads.data();
    thread = std::async( std::launch::async, get_task(index) );
  }
  get_task( threads.size() )();
  for (auto& thread : threads) {
    thread.get();
  }
}

This is a little multi threading library.

You use it like this:

do_tasks( 100, [&](size_t start, size_t finish) {
  // do subtasks starting at index start, up to and not including finish
});

There are other more complex threading libraries, but writing a small half-decent one isn't hard so I did it.

To be explicit:

Photon photon;
do_tasks( 1000000, [&](size_t start, size_t finish) {
  for (int i = start; i < finish; ++i) {
    photon.lanuch(args...){
  }
});

but you'll have to be extremely careful making sure there is no unsafe data sharing between the threads, and you aren't just blocking each thread on a common mutex.

Live example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i refactor these lines of code?

How can I refactor this code to remove duplication?

How can I refactor the code with many unions

How can I refactor this code in javascript?

How can I refactor API code in Javascript?

How do I refactor my code to call AppDelegate on the main thread?

I use many "OR" operator how can i refactor code in JAVA

How can I refactor my kotlin code and make it more clear

Avoiding reflection - How best can I refactor this code?

How can I use ESLint to refactor/restyle code in webstorm

How can I use the Factory pattern to refactor my Java code?

How can I refactor this code snippet to make it more efficient?

How can I refactor file names in Visual Studio Code?

How can I refactor this code for fetching Mongo Data from an Array?

How can I store key presses into variables to refactor this code?

How can I refactor repetitive conditional Vue.js code?

How can I refactor these two functions with repeating code?

How can I refactor my code for this coding challenge?

How can I refactor this Javascript code to make it more modular and concise?

How can I refactor / make this code more pythonic?

Rails: How can i refactor my controller params code

How can i refactor these functions?

Can I refactor this code in any way in javascript?

How could I refactor well better this code?

How can i get multi thread parallel calculator (factorial sum)

how can i use static method in php multi thread

how can I make thread in TCP server multi client dialogue

how can I refactor methods in Javascript

How can I refactor this if elif statements?