• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 5
12 #define BOOST_THREAD_USES_LOG_THREAD_ID
13 
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>
19 #include <string>
20 #include <iostream>
21 
22 #include <boost/thread/caller_context.hpp>
23 
24 struct current_executor_state_type {
25   boost::shared_ptr<boost::generic_executor_ref> current_executor_ptr;
26 
27   template <class Executor>
set_current_executorcurrent_executor_state_type28   void set_current_executor(Executor& ex)
29   {
30     current_executor_ptr = boost::make_shared<boost::generic_executor_ref>(ex);
31   }
current_executorcurrent_executor_state_type32   boost::generic_executor_ref current_executor()
33   {
34     if (current_executor_ptr)
35       return *current_executor_ptr;
36     else
37       throw "";
38   }
39 };
40 
41 thread_local current_executor_state_type current_executor_state;
42 
current_executor()43 boost::generic_executor_ref current_executor()
44 {
45   return current_executor_state.current_executor();
46 }
47 
p2()48 void p2()
49 {
50   std::cout << BOOST_CONTEXTOF << std::endl;
51   boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
52   std::cout << BOOST_CONTEXTOF << std::endl;
53 }
54 
55 
p1()56 void p1()
57 {
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;
63 }
64 
main()65 int main()
66 {
67   std::cout << BOOST_CONTEXTOF << std::endl;
68 
69   boost::basic_thread_pool tp(4,
70       // at_thread_entry
71       [](boost::basic_thread_pool& pool)
72       {
73         current_executor_state.set_current_executor(pool);
74       }
75   );
76 
77   tp.submit(&p1);
78 
79   boost::this_thread::sleep_for(boost::chrono::seconds(5));
80 
81   std::cout << BOOST_CONTEXTOF << std::endl;
82 
83   return 1;
84 
85 }
86