Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #56 Launch 1000 parallel tasks and wait for completion

Fork-join : launch the concurrent execution of procedure f with parameter i from 1 to 1000.
Tasks are independent and f(i) doesn't return any value.
Tasks need not run all at the same time, so you may use a pool.
Wait for the completion of the 1000 tasks and then print "Finished".

#include<latch>
#include<thread>
size_t taskCount = 1000;
auto remainingTasks = std::latch(static_cast<std::ptrdiff_t>(taskCount));
auto f = [&remainingTasks](int i) {
  // do work
  remainingTasks.count_down();
};

std::vector<std::jthread> threads{ 10 };
for (auto i = 0; i < taskCount; i++) {
  auto& workerThread = threads[i % 10];
  if (workerThread.joinable()) {
    workerThread.join();
  }
  workerThread = std::jthread(f, i);
}

remainingTasks.wait();
std::cout << "Finished";

Latch is used to wait until all tasks are completed. Simulated thread pool with vector of jthreads.
using System.Threading.Tasks;
using System.Linq;
await Task.WhenAll(Enumerable.Range(0, 1000).Select(i => Task.Run(() => f(i))));
Console.WriteLine("Finished");

static async Task f(int value)
{
    // Do something with value. Delay to simulate work
    await Task.Delay(500);
}

Using the async/await pattern

New implementation...