How to parallel execution to multiple independent tasks in C++

1 Answer

0 votes
#include <future>
#include <vector>
#include <iostream>

void func(int i) {
    std::cout << i << "\n";
}

int main()
{
    auto futures = std::vector<std::future<void>>{};

    for (auto i = 1; i <= 15; i++) {
        futures.emplace_back(std::async(func, i));
    }
}




/*
run:

2
3
7
6
5
4
1
8
15
9
13
12
11
10
14

*/

 



answered Mar 4, 2023 by avibootz
...