qwerty787788

std::vector<bool> async

Oct 13th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <future>
  3. #include <vector>
  4.  
  5. constexpr int N = (int) 1e7;
  6. constexpr int threads = 10;
  7.  
  8. std::vector<bool> all(N);
  9.  
  10. int main() {
  11.     std::vector<std::future<void>> futures;
  12.     for (int it = 0; it < threads; it++) {
  13.         auto f = std::async(std::launch::async, [it]{
  14.             for (int i =0 ; i < N; i++) {
  15.                 if (i % threads == it) {
  16.                     all[i] = true;
  17.                 }
  18.             }
  19.         });
  20.         futures.push_back(std::move(f));
  21.     }
  22.     for (auto &f : futures) {
  23.         f.get();
  24.     }
  25.     int sum = 0;
  26.     for (bool z : all) {
  27.         sum += (z ? 1 : 0);
  28.     }
  29.     std::cerr << sum << std::endl;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment