Advertisement
Guest User

vs2011 std::async is buggy

a guest
Apr 15th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 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(const std::string& func, int id) : func_(func), id_(id) {
  11.         std::cout << func_ << " begin, id: " << id_ << std::endl;
  12.         timer_.restart();
  13.     }
  14.     ~Trace() {
  15.         std::cout << func_ << " end,   id: " << id_ << ", dur: " << timer_.elapsed() << "s" << std::endl;
  16.     }
  17.  
  18.     int id_;
  19.     std::string func_;
  20.     boost::timer    timer_;
  21. };
  22.  
  23. int dummyExpensiveBlockingNetworkCall() {
  24.  
  25.     int id = boost::lexical_cast<int>(std::this_thread::get_id());
  26.  
  27.     Trace trace("net", id);
  28.  
  29.     int result = 0;
  30.    
  31.     boost::timer    tm;
  32.  
  33.     while(tm.elapsed() < 10.0) {
  34.         ++result; //Busy sleep. Changing this to a "normal" sleep changes behaviour.
  35.     }
  36.  
  37.     return result;
  38. }
  39.  
  40. void slow() {
  41.     int id = boost::lexical_cast<int>(std::this_thread::get_id());
  42.     Trace trace("slow", id);
  43.  
  44.     std::vector<std::future<int>> futures;
  45.     for (int i = 0; i < 10; ++i)
  46.     {
  47.         auto fut = std::async(std::launch::async, [=]()
  48.         {
  49.             return dummyExpensiveBlockingNetworkCall();
  50.         });
  51.         futures.push_back(std::move(fut));
  52.     }
  53.  
  54.     std::vector<int> resVec;
  55.     std::for_each(futures.begin(), futures.end(), [&](std::future<int> & fut)
  56.     {
  57.         resVec.push_back(fut.get());
  58.     });
  59.  
  60.     /*
  61.     std::for_each(resVec.begin(), resVec.end(), [](int result) {
  62.         std::cout << result << ", ";
  63.     });
  64.     */
  65.  
  66. }
  67.  
  68. void fast() {
  69.     int id = boost::lexical_cast<int>(std::this_thread::get_id());
  70.     Trace trace("fast", id);
  71.  
  72.     std::vector<std::thread> threads;
  73.     for (int i = 0; i < 10; ++i)
  74.     {
  75.         auto th = std::thread([=]()
  76.         {
  77.             dummyExpensiveBlockingNetworkCall();
  78.         });
  79.         threads.push_back(std::move(th));
  80.     }
  81.  
  82.     std::vector<int> resVec;
  83.     std::for_each(threads.begin(), threads.end(), [&](std::thread& th)
  84.     {
  85.         th.join();
  86.     });
  87. }
  88.  
  89. int main()
  90. {
  91.  
  92.     slow(); //buggy, reuses threads even though it should not.
  93.     /*
  94.     output:
  95.         slow begin, id: 968
  96.         net begin, id: 968
  97.         net begin, id: 3184
  98.         net begin, id: 5076
  99.         netnet end,   id: 968, dur: 10s
  100.          end,   id: 5076, dur: 10s
  101.         net begin, id: 5076
  102.         net end,   id: 3184, dur: 10s
  103.         net begin, id: 3184
  104.         net end,   id: 5076, dur: 10s
  105.         net begin, id: 5076
  106.         net end,   id: 3184, dur: 10s
  107.         net begin, id: 3184
  108.         net end,   id: 5076, dur: 10s
  109.         net begin, id: 5076
  110.         net end,   id: 3184, dur: 10s
  111.         net begin, id: 3184
  112.         net end,   id: 5076, dur: 10.003s
  113.         net begin, id: 5076
  114.         net end,   id: 3184, dur: 10.005s
  115.         net end,   id: 5076, dur: 10s
  116.         slow end,   id: 968, dur: 50.017s
  117.     */
  118.  
  119.     //fast(); //correct
  120.     /*
  121.     output:
  122.         fast begin, id: 4124
  123.         net begin, id: 2476
  124.         net begin, id: 2684
  125.         net begin, id: net begin, id: 2848
  126.         net begin, id: 4724
  127.         net begin, id: 1048
  128.         net begin, id: 4080
  129.         net begin, id: 1364
  130.         net begin, id: 1776
  131.         3140
  132.         net begin, id: 2000
  133.         net end,   id: 2684, dur: 10.009s
  134.         net end,   id: net end,   id: 2848, dur: 10.045s
  135.         net end,   id: 3140, dur: 10.003s
  136.         net end,   id: 2000, dur: 10.001s
  137.         net end,   id: 1364, dur: 10.006s
  138.         net end,   id: 4080, dur: 10.011s
  139.         net end,   id: 1776, dur: 10.004s
  140.         2476, dur: 10.211snet end,   id: 4724, dur: 10.004s
  141.  
  142.         net end,   id: 1048, dur: 10.012s
  143.         fast end,   id: 4124, dur: 11.375s
  144.     */
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement