| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,61 @@ | |||
| 1 | + // Copyright (C) 2014 Vicente Botet | ||
| 2 | + // | ||
| 3 | + // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 4 | + // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 5 | + | ||
| 6 | + #include <boost/config.hpp> | ||
| 7 | + #if ! defined BOOST_NO_CXX11_DECLTYPE | ||
| 8 | + #define BOOST_RESULT_OF_USE_DECLTYPE | ||
| 9 | + #endif | ||
| 10 | + | ||
| 11 | + #define BOOST_THREAD_VERSION 4 | ||
| 12 | + #define BOOST_THREAD_PROVIDES_EXECUTORS | ||
| 13 | + //#define BOOST_THREAD_USES_LOG | ||
| 14 | + #define BOOST_THREAD_USES_LOG_THREAD_ID | ||
| 15 | + #define BOOST_THREAD_QUEUE_DEPRECATE_OLD | ||
| 16 | + | ||
| 17 | + #include <boost/thread/caller_context.hpp> | ||
| 18 | + #include <boost/thread/executors/basic_thread_pool.hpp> | ||
| 19 | + #include <boost/thread/executors/generic_executor_ref.hpp> | ||
| 20 | + #include <string> | ||
| 21 | + #include <iostream> | ||
| 22 | + | ||
| 23 | + #include <boost/thread/caller_context.hpp> | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + boost::generic_executor_ref default_executor() | ||
| 27 | + { | ||
| 28 | + static boost::basic_thread_pool tp(4); | ||
| 29 | + return boost::generic_executor_ref(tp); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + void p2() | ||
| 33 | + { | ||
| 34 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 35 | + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); | ||
| 36 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + void p1() | ||
| 41 | + { | ||
| 42 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 43 | + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); | ||
| 44 | + default_executor().submit(&p2); | ||
| 45 | + boost::this_thread::sleep_for(boost::chrono::milliseconds(400)); | ||
| 46 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + int main() | ||
| 50 | + { | ||
| 51 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 52 | + | ||
| 53 | + default_executor().submit(&p1); | ||
| 54 | + | ||
| 55 | + boost::this_thread::sleep_for(boost::chrono::seconds(5)); | ||
| 56 | + | ||
| 57 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 58 | + | ||
| 59 | + return 1; | ||
| 60 | + | ||
| 61 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,86 @@ | |||
| 1 | + // Copyright (C) 2014 Vicente Botet | ||
| 2 | + // | ||
| 3 | + // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 4 | + // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 5 | + | ||
| 6 | + #include <boost/config.hpp> | ||
| 7 | + #if ! defined BOOST_NO_CXX11_DECLTYPE | ||
| 8 | + #define BOOST_RESULT_OF_USE_DECLTYPE | ||
| 9 | + #endif | ||
| 10 | + | ||
| 11 | + #define BOOST_THREAD_VERSION 4 | ||
| 12 | + #define BOOST_THREAD_PROVIDES_EXECUTORS | ||
| 13 | + #define BOOST_THREAD_USES_LOG_THREAD_ID | ||
| 14 | + | ||
| 15 | + #include <boost/thread/caller_context.hpp> | ||
| 16 | + #include <boost/thread/executors/basic_thread_pool.hpp> | ||
| 17 | + #include <boost/thread/executors/generic_executor_ref.hpp> | ||
| 18 | + #include <boost/smart_ptr/shared_ptr.hpp> | ||
| 19 | + #include <boost/smart_ptr/make_shared.hpp> | ||
| 20 | + #include <string> | ||
| 21 | + #include <iostream> | ||
| 22 | + | ||
| 23 | + #include <boost/thread/caller_context.hpp> | ||
| 24 | + | ||
| 25 | + struct current_executor_state_type { | ||
| 26 | + boost::shared_ptr<boost::generic_executor_ref> current_executor_ptr; | ||
| 27 | + | ||
| 28 | + template <class Executor> | ||
| 29 | + void set_current_executor(Executor& ex) | ||
| 30 | + { | ||
| 31 | + current_executor_ptr = boost::make_shared<boost::generic_executor_ref>(ex); | ||
| 32 | + } | ||
| 33 | + boost::generic_executor_ref current_executor() | ||
| 34 | + { | ||
| 35 | + if (current_executor_ptr) | ||
| 36 | + return *current_executor_ptr; | ||
| 37 | + else | ||
| 38 | + throw ""; | ||
| 39 | + } | ||
| 40 | + }; | ||
| 41 | + | ||
| 42 | + thread_local current_executor_state_type current_executor_state; | ||
| 43 | + | ||
| 44 | + boost::generic_executor_ref current_executor() | ||
| 45 | + { | ||
| 46 | + return current_executor_state.current_executor(); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + void p2() | ||
| 50 | + { | ||
| 51 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 52 | + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); | ||
| 53 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + void p1() | ||
| 58 | + { | ||
| 59 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 60 | + boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); | ||
| 61 | + current_executor().submit(&p2); | ||
| 62 | + boost::this_thread::sleep_for(boost::chrono::milliseconds(400)); | ||
| 63 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + int main() | ||
| 67 | + { | ||
| 68 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 69 | + | ||
| 70 | + boost::basic_thread_pool tp(4, | ||
| 71 | + // at_thread_entry | ||
| 72 | + [](boost::basic_thread_pool& pool) | ||
| 73 | + { | ||
| 74 | + current_executor_state.set_current_executor(pool); | ||
| 75 | + } | ||
| 76 | + ); | ||
| 77 | + | ||
| 78 | + tp.submit(&p1); | ||
| 79 | + | ||
| 80 | + boost::this_thread::sleep_for(boost::chrono::seconds(5)); | ||
| 81 | + | ||
| 82 | + std::cout << BOOST_CONTEXTOF << std::endl; | ||
| 83 | + | ||
| 84 | + return 1; | ||
| 85 | + | ||
| 86 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments