Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <future>
- #include <thread>
- #include <boost/timer.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- struct Trace {
- Trace(const std::string& func, int id) : func_(func), id_(id) {
- std::cout << func_ << " begin, id: " << id_ << std::endl;
- timer_.restart();
- }
- ~Trace() {
- std::cout << func_ << " end, id: " << id_ << ", dur: " << timer_.elapsed() << "s" << std::endl;
- }
- int id_;
- std::string func_;
- boost::timer timer_;
- };
- int dummyExpensiveBlockingNetworkCall() {
- int id = boost::lexical_cast<int>(std::this_thread::get_id());
- Trace trace("net", id);
- int result = 0;
- boost::timer tm;
- while(tm.elapsed() < 10.0) {
- ++result; //Busy sleep. Changing this to a "normal" sleep changes behaviour.
- }
- return result;
- }
- void slow() {
- int id = boost::lexical_cast<int>(std::this_thread::get_id());
- Trace trace("slow", id);
- std::vector<std::future<int>> futures;
- for (int i = 0; i < 10; ++i)
- {
- auto fut = std::async(std::launch::async, [=]()
- {
- return dummyExpensiveBlockingNetworkCall();
- });
- futures.push_back(std::move(fut));
- }
- std::vector<int> resVec;
- std::for_each(futures.begin(), futures.end(), [&](std::future<int> & fut)
- {
- resVec.push_back(fut.get());
- });
- /*
- std::for_each(resVec.begin(), resVec.end(), [](int result) {
- std::cout << result << ", ";
- });
- */
- }
- void fast() {
- int id = boost::lexical_cast<int>(std::this_thread::get_id());
- Trace trace("fast", id);
- std::vector<std::thread> threads;
- for (int i = 0; i < 10; ++i)
- {
- auto th = std::thread([=]()
- {
- dummyExpensiveBlockingNetworkCall();
- });
- threads.push_back(std::move(th));
- }
- std::vector<int> resVec;
- std::for_each(threads.begin(), threads.end(), [&](std::thread& th)
- {
- th.join();
- });
- }
- int main()
- {
- slow(); //buggy, reuses threads even though it should not.
- /*
- output:
- slow begin, id: 968
- net begin, id: 968
- net begin, id: 3184
- net begin, id: 5076
- netnet end, id: 968, dur: 10s
- end, id: 5076, dur: 10s
- net begin, id: 5076
- net end, id: 3184, dur: 10s
- net begin, id: 3184
- net end, id: 5076, dur: 10s
- net begin, id: 5076
- net end, id: 3184, dur: 10s
- net begin, id: 3184
- net end, id: 5076, dur: 10s
- net begin, id: 5076
- net end, id: 3184, dur: 10s
- net begin, id: 3184
- net end, id: 5076, dur: 10.003s
- net begin, id: 5076
- net end, id: 3184, dur: 10.005s
- net end, id: 5076, dur: 10s
- slow end, id: 968, dur: 50.017s
- */
- //fast(); //correct
- /*
- output:
- fast begin, id: 4124
- net begin, id: 2476
- net begin, id: 2684
- net begin, id: net begin, id: 2848
- net begin, id: 4724
- net begin, id: 1048
- net begin, id: 4080
- net begin, id: 1364
- net begin, id: 1776
- 3140
- net begin, id: 2000
- net end, id: 2684, dur: 10.009s
- net end, id: net end, id: 2848, dur: 10.045s
- net end, id: 3140, dur: 10.003s
- net end, id: 2000, dur: 10.001s
- net end, id: 1364, dur: 10.006s
- net end, id: 4080, dur: 10.011s
- net end, id: 1776, dur: 10.004s
- 2476, dur: 10.211snet end, id: 4724, dur: 10.004s
- net end, id: 1048, dur: 10.012s
- fast end, id: 4124, dur: 11.375s
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement