#include "stdafx.h" #define BOOST_THREAD_VERSION 4 #define BOOST_THREAD_PROVIDES_EXECUTORS #define BOOST_THREAD_USES_LOG_THREAD_ID #define BOOST_THREAD_QUEUE_DEPRECATE_OLD #include // for lexical_cast #include #include // for BOOST_CONTEXTOF, caller_context_t, operator<< #include // for basic_thread_pool #include #include #include #include #include // for executor #include // for executor_adaptor #include // for operator<<, cout, endl, .. #include // for operator "" ms, operator "" s #include // for sleep_for #include #include #include template static inline void trace(A const &a, B const& b = "") { static boost::mutex stdout_mutex; boost::lock_guard lock(stdout_mutex); std::cout << a << b << std::endl; } static inline void trace(boost::caller_context_t const &ctx) { trace(boost::lexical_cast(ctx)); } namespace { using namespace std::chrono_literals; using std::this_thread::sleep_for; void p1() { trace(BOOST_CONTEXTOF); sleep_for(20ms); } void p2() { trace(BOOST_CONTEXTOF); sleep_for(1s); } int f1() { trace(BOOST_CONTEXTOF); sleep_for(100ms); return 1; } /* template ///////////////////// int f2(boost::executor_adaptor & exec_ad) { trace(exex_ad.underlying_executor(),BOOST_CONTEXTOF); sleep_for(100ms); return 1; } */ } void submit_some(boost::executor &tp) { for (int i = 0; i < 3; ++i) { tp.submit(p2); } for (int i = 0; i < 3; ++i) { tp.submit(p1); } } void at_th_entry(boost::basic_thread_pool&) { } int test_executor_adaptor() { trace(BOOST_CONTEXTOF); try { boost::executor_adaptor ea(4); boost::executor_adaptor < boost::basic_thread_pool > ea00(4); trace(BOOST_CONTEXTOF); submit_some(ea); { boost::future t1 = boost::async(ea, &f1); ////boost::future t1 = boost::async(ea, &boost::bind(&f2,ea));////////// trace(BOOST_CONTEXTOF); trace(" t1= ", t1.get()); // problem unknown on running showing thread result before running code submit_some(ea00); boost::future t2 = boost::async(ea, &f1);///now the problem is clear,when async starts workqueue of ea it also starts workqueue of ea00//////?????????????? trace(BOOST_CONTEXTOF); trace(" t2= ", t2.get()); // problem unknown on running showing thread result before running code /* std::vector > vt1; for (int i = 0; i < 4; i++) { vt1.push_back(boost::async(ea, &f1)); // here async starts all closures already submitted to ea // then submit f1 to all threads in ea to work // asynchronusly so end result will be 7 already submitted // and 4 more f1 and return futures to only the last 4 f1 // which is submitted by async } for (auto &e : vt1) { auto e_value = e.get(); trace("vt1 e_value = ", e_value); } */ int x = 0; } } catch (std::exception &ex) { trace("ERROR= ", ex.what()); return 1; } catch (...) { trace("UNKNOWN EXCEPTION"); return 2; } trace(BOOST_CONTEXTOF); return 0; } int main() { trace(BOOST_CONTEXTOF); return test_executor_adaptor(); }