Advertisement
Guest User

vs2011 std::async(std::launch::async, ...) broken

a guest
Apr 14th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <future>
  4. #include <thread>
  5.  
  6. #include <boost/timer.hpp>
  7. #include <boost/date_time/posix_time/posix_time.hpp>
  8.  
  9. struct Trace {
  10.     Trace(int id) : id_(id) {
  11.         std::cout << " begin " << id_ << std::endl;
  12.     }
  13.     ~Trace() {
  14.         std::cout << " end " << id_ << std::endl;
  15.     }
  16.  
  17.     int id_;
  18. };
  19.  
  20.  
  21. int dummyExpensiveBlockingNetworkCall(int id) {
  22.     Trace trace(id);
  23.  
  24.     int result = 0;
  25.    
  26.     boost::timer    tm;
  27.  
  28.     while(tm.elapsed() < 10.0) {
  29.         ++result; //busy sleep
  30.     }
  31.  
  32.     return result;
  33. }
  34.  
  35. void slow() {
  36.     std::vector<std::future<int>> futures;
  37.     for (int i = 0; i < 10; ++i)
  38.     {
  39.         auto fut = std::async(std::launch::async, [=]()
  40.         {
  41.             return dummyExpensiveBlockingNetworkCall(i);
  42.         });
  43.         futures.push_back(std::move(fut));
  44.     }
  45.  
  46.     std::vector<int> resVec;
  47.     std::for_each(futures.begin(), futures.end(), [&](std::future<int> & fut)
  48.     {
  49.         resVec.push_back(fut.get());
  50.     });
  51.  
  52.     /*
  53.     std::for_each(resVec.begin(), resVec.end(), [](int result) {
  54.         std::cout << result << ", ";
  55.     });
  56.     */
  57.  
  58. }
  59.  
  60. void fast() {
  61.     std::vector<std::thread> threads;
  62.     for (int i = 0; i < 10; ++i)
  63.     {
  64.         auto th = std::thread([=]()
  65.         {
  66.             dummyExpensiveBlockingNetworkCall(i);
  67.         });
  68.         threads.push_back(std::move(th));
  69.     }
  70.  
  71.     std::vector<int> resVec;
  72.     std::for_each(threads.begin(), threads.end(), [&](std::thread& th)
  73.     {
  74.         th.join();
  75.     });
  76. }
  77.  
  78. int main()
  79. {
  80.     slow(); //takes up to 100 seconds as some work does not start before others have finsihed. As in the work is not paralell even thoough we told it to use std::launch::async.
  81.     /*
  82.     output:
  83.          begin 0
  84.          begin 1
  85.          begin 2
  86.          end 0
  87.          end  end 1
  88.          begin 3
  89.         2
  90.          begin 4
  91.          end 3
  92.          end 4
  93.          begin 5
  94.          begin 7
  95.          begin 6
  96.          end 7
  97.          begin 8
  98.          end 6
  99.          end 5
  100.          begin 9
  101.          end 8
  102.          end 9 
  103.     */
  104.  
  105.     //fast(); //takes at most 10 seconds as all begin come before end. Work is truly parallell
  106.     /*
  107.         output:
  108.          begin 0
  109.          begin 1
  110.          begin 2
  111.          begin 3
  112.          begin 4
  113.          begin 5
  114.          begin 6 begin 8
  115.          begin 7
  116.  
  117.          begin 9
  118.          end 0 end 2
  119.          end 5
  120.          end 1
  121.          end 3
  122.          end 4
  123.  
  124.          end  end 7
  125.          end 6
  126.         8
  127.          end 9 
  128. */
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement