1 // Copyright (C) 2014 Vicente Botet
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)
6 #include <boost/config.hpp>
7 #if ! defined BOOST_NO_CXX11_DECLTYPE
8 #define BOOST_RESULT_OF_USE_DECLTYPE
11 #define BOOST_THREAD_VERSION 5
12 #define BOOST_THREAD_USES_LOG_THREAD_ID
14 #include <boost/thread/caller_context.hpp>
15 #include <boost/thread/executors/basic_thread_pool.hpp>
16 #include <boost/thread/executors/generic_executor_ref.hpp>
17 #include <boost/smart_ptr/shared_ptr.hpp>
18 #include <boost/smart_ptr/make_shared.hpp>
22 #include <boost/thread/caller_context.hpp>
24 struct current_executor_state_type
{
25 boost::shared_ptr
<boost::generic_executor_ref
> current_executor_ptr
;
27 template <class Executor
>
28 void set_current_executor(Executor
& ex
)
30 current_executor_ptr
= boost::make_shared
<boost::generic_executor_ref
>(ex
);
32 boost::generic_executor_ref
current_executor()
34 if (current_executor_ptr
)
35 return *current_executor_ptr
;
41 thread_local current_executor_state_type current_executor_state
;
43 boost::generic_executor_ref
current_executor()
45 return current_executor_state
.current_executor();
50 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
51 boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
52 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
58 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
59 boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
60 current_executor().submit(&p2
);
61 boost::this_thread::sleep_for(boost::chrono::milliseconds(400));
62 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
67 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
69 boost::basic_thread_pool
tp(4,
71 [](boost::basic_thread_pool
& pool
)
73 current_executor_state
.set_current_executor(pool
);
79 boost::this_thread::sleep_for(boost::chrono::seconds(5));
81 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;