From 46fb6ced302b941275a1178c398ecb8ba66b469f Mon Sep 17 00:00:00 2001 From: q66 Date: Sat, 23 Jan 2016 17:26:42 +0000 Subject: [PATCH] support for extra args for thread constructor --- ostd/thread.hh | 26 +++++++++++++++++++++----- ostd/tuple.hh | 7 +++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ostd/thread.hh b/ostd/thread.hh index 4b75a3f..ca62441 100644 --- a/ostd/thread.hh +++ b/ostd/thread.hh @@ -11,6 +11,8 @@ #include "ostd/memory.hh" #include "ostd/platform.hh" +#include "ostd/type_traits.hh" +#include "ostd/tuple.hh" namespace ostd { @@ -72,10 +74,21 @@ namespace this_thread { } namespace detail { + template + inline Decay decay_copy(T &&v) { + return forward(v); + } + + template + inline void thread_exec(Tuple &tup, detail::TupleIndices) { + ostd::get<0>(tup)(ostd::move(ostd::get(tup))...); + } + template inline int thread_proxy(void *ptr) { Box fptr((F *)ptr); - (*fptr)(); + using Index = detail::MakeTupleIndices, 1>; + detail::thread_exec(*fptr, Index()); return 0; } } @@ -86,10 +99,13 @@ struct Thread { Thread(): p_thread(0) {} Thread(Thread &&o): p_thread(o.p_thread) { o.p_thread = 0; } - template - Thread(F &&func) { - Box p(new F(func)); - int res = thrd_create(&p_thread, &detail::thread_proxy, p.get()); + template, Thread>>> + Thread(F &&func, A &&...args) { + using FuncT = Tuple, Decay...>; + Box p(new FuncT(detail::decay_copy(forward(func)), + detail::decay_copy(forward(args))...)); + int res = thrd_create(&p_thread, &detail::thread_proxy, p.get()); if (res == thrd_success) p.release(); else diff --git a/ostd/tuple.hh b/ostd/tuple.hh index bd97e83..5702314 100644 --- a/ostd/tuple.hh +++ b/ostd/tuple.hh @@ -177,8 +177,11 @@ namespace detail { template inline void tuple_swallow(A &&...) {} - template constexpr bool TupleAll - = IsSame, TupleAll<(A, true)...>>; + template + constexpr bool TupleAll = true; + + template + constexpr bool TupleAll = B && TupleAll; template constexpr bool TupleAllDefaultConstructible = detail::Undefined();