Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <future>
- #include <vector>
- constexpr int N = (int) 1e7;
- constexpr int threads = 10;
- std::vector<bool> all(N);
- int main() {
- std::vector<std::future<void>> futures;
- for (int it = 0; it < threads; it++) {
- auto f = std::async(std::launch::async, [it]{
- for (int i =0 ; i < N; i++) {
- if (i % threads == it) {
- all[i] = true;
- }
- }
- });
- futures.push_back(std::move(f));
- }
- for (auto &f : futures) {
- f.get();
- }
- int sum = 0;
- for (bool z : all) {
- sum += (z ? 1 : 0);
- }
- std::cerr << sum << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment