Advertisement
Guest User

maxpp::posix::threads::thread::thread

a guest
Dec 20th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. template <typename F, typename Tuple, size_t... I>
  2. decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>) {
  3.     return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
  4. }
  5.  
  6. template <typename F, typename Tuple>
  7. decltype(auto) apply(F&& f, Tuple&& t) {
  8.     using Indices = std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>;
  9.     return apply_impl(std::forward<F>(f), std::forward<Tuple>(t), Indices{});
  10. }
  11.  
  12.  
  13. template <typename Function, typename... Args>
  14. void* helperf(void* params) {
  15.     typedef std::tuple<Function, std::tuple<Args...>> hell;
  16.     std::unique_ptr<hell> ptr(static_cast<hell*>(params));
  17.     apply(std::move(std::get<0>(*ptr)), std::move(std::get<1>(*ptr)));
  18.     return nullptr;
  19. }
  20.  
  21. template <typename Function, typename... Args>
  22. thread::thread(Function&& f, Args&&... args) {
  23.     typedef std::tuple<Function, std::tuple<Args...>> hell;
  24.     auto topass = std::unique_ptr<hell>(
  25.         new hell(std::move(f), std::forward_as_tuple<Args...>(std::forward<Args...>(args...))));
  26.     pthread_create(&_thread, NULL, &helperf<Function, Args...>, topass.release());
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement