JayBeams  0.1
Another project to have fun coding.
thread_setup_wrapper.hpp
Go to the documentation of this file.
1 #ifndef jb_detail_thread_setup_wrapper_hpp
2 #define jb_detail_thread_setup_wrapper_hpp
3 
5 #include <jb/log.hpp>
6 #include <functional>
7 #include <thread>
8 
9 namespace jb {
10 namespace detail {
11 
12 /**
13  * Hold data to startup a thread in JayBeams.
14  *
15  * We want to launch all threads using a common configuration
16  * function. To match the semantics of std::thread, we need to copy
17  * the thread functor and its parameters exactly once. This class
18  * wraps the relevant bits in a shared_ptr<> to avoid excessive
19  * copying.
20  */
21 template <typename Callable>
23 public:
24  thread_setup_wrapper(jb::thread_config const& config, Callable&& c)
25  : config_(config)
26  , callable_(std::make_shared<Callable>(std::forward<Callable>(c))) {
27  }
28  virtual void operator()() {
29  try {
31  (*callable_)();
32  } catch (std::exception const& ex) {
33  JB_LOG(warning) << "standard exception raised: " << ex.what()
34  << " in thread reconfiguration";
35  } catch (...) {
36  JB_LOG(warning) << "unknown exception raised"
37  << " in thread reconfiguration";
38  }
39  }
40 
41 private:
43  std::shared_ptr<Callable> callable_;
44 };
45 
46 /**
47  * Create the right type of thread_setup_wrapper<>.
48  */
49 template <typename Callable>
51 make_thread_setup_wrapper(thread_config const& config, Callable&& c) {
52  return thread_setup_wrapper<Callable>(config, std::forward<Callable>(c));
53 }
54 
55 } // namespace detail
56 } // namespace jb
57 
58 #endif // jb_detail_thread_setup_wrapper_hpp
thread_setup_wrapper(jb::thread_config const &config, Callable &&c)
STL namespace.
Hold the configuration to initialize threads.
Hold data to startup a thread in JayBeams.
void reconfigure_this_thread(thread_config const &config)
Change the current thread parameters based on the configuration.
std::shared_ptr< Callable > callable_
#define JB_LOG(lvl)
Definition: log.hpp:70
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7
thread_setup_wrapper< Callable > make_thread_setup_wrapper(thread_config const &config, Callable &&c)
Create the right type of thread_setup_wrapper<>.