boost asio异步代码
#include <iostream> #include <boost/asio.hpp> #include <boost/thread.hpp> void print(const int data) { if (data == 99999) std::cout << data << std::endl; } int main() { boost::asio::io_service io_service; boost::asio::io_service::work work(io_service); UINT tm1 = GetTickCount(); for (int i = 0;i < 100000; i++) { io_service.post(std::bind(&print, i)); } UINT tm2 = GetTickCount(); std::cout << "cost:" << tm2 - tm1 << std::endl; try { boost::thread([&]() { io_service.run(); }); } catch (...) { } getchar(); return 0; }
结果打印:
stl异步代码:
#include <iostream> #include <future> #include <Windows.h> void print(const int data) { if (data == 99999) std::cout << data << std::endl; } int main() { UINT tm1 = GetTickCount(); for (int i = 0;i < 100000; i++) { std::async(std::bind(&print, i)); } UINT tm2 = GetTickCount(); std::cout << "cost:" << tm2 - tm1 << std::endl; getchar(); return 0; }
输出结果如下: