forked from OctaForge/libostd
support for extra args for thread constructor
parent
b84eb137da
commit
46fb6ced30
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
#include "ostd/memory.hh"
|
#include "ostd/memory.hh"
|
||||||
#include "ostd/platform.hh"
|
#include "ostd/platform.hh"
|
||||||
|
#include "ostd/type_traits.hh"
|
||||||
|
#include "ostd/tuple.hh"
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
|
|
||||||
|
@ -72,10 +74,21 @@ namespace this_thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
template<typename T>
|
||||||
|
inline Decay<T> decay_copy(T &&v) {
|
||||||
|
return forward<T>(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename F, typename ...A, Size ...I>
|
||||||
|
inline void thread_exec(Tuple<F, A...> &tup, detail::TupleIndices<I...>) {
|
||||||
|
ostd::get<0>(tup)(ostd::move(ostd::get<I>(tup))...);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
inline int thread_proxy(void *ptr) {
|
inline int thread_proxy(void *ptr) {
|
||||||
Box<F> fptr((F *)ptr);
|
Box<F> fptr((F *)ptr);
|
||||||
(*fptr)();
|
using Index = detail::MakeTupleIndices<TupleSize<F>, 1>;
|
||||||
|
detail::thread_exec(*fptr, Index());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,10 +99,13 @@ struct Thread {
|
||||||
Thread(): p_thread(0) {}
|
Thread(): p_thread(0) {}
|
||||||
Thread(Thread &&o): p_thread(o.p_thread) { o.p_thread = 0; }
|
Thread(Thread &&o): p_thread(o.p_thread) { o.p_thread = 0; }
|
||||||
|
|
||||||
template<typename F>
|
template<typename F, typename ...A,
|
||||||
Thread(F &&func) {
|
typename = EnableIf<!IsSame<Decay<F>, Thread>>>
|
||||||
Box<F> p(new F(func));
|
Thread(F &&func, A &&...args) {
|
||||||
int res = thrd_create(&p_thread, &detail::thread_proxy<F>, p.get());
|
using FuncT = Tuple<Decay<F>, Decay<A>...>;
|
||||||
|
Box<FuncT> p(new FuncT(detail::decay_copy(forward<F>(func)),
|
||||||
|
detail::decay_copy(forward<A>(args))...));
|
||||||
|
int res = thrd_create(&p_thread, &detail::thread_proxy<FuncT>, p.get());
|
||||||
if (res == thrd_success)
|
if (res == thrd_success)
|
||||||
p.release();
|
p.release();
|
||||||
else
|
else
|
||||||
|
|
|
@ -177,8 +177,11 @@ namespace detail {
|
||||||
template<typename ...A>
|
template<typename ...A>
|
||||||
inline void tuple_swallow(A &&...) {}
|
inline void tuple_swallow(A &&...) {}
|
||||||
|
|
||||||
template<bool ...A> constexpr bool TupleAll
|
template<bool ...A>
|
||||||
= IsSame<TupleAll<A...>, TupleAll<(A, true)...>>;
|
constexpr bool TupleAll = true;
|
||||||
|
|
||||||
|
template<bool B, bool ...A>
|
||||||
|
constexpr bool TupleAll<B, A...> = B && TupleAll<A...>;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr bool TupleAllDefaultConstructible = detail::Undefined<T>();
|
constexpr bool TupleAllDefaultConstructible = detail::Undefined<T>();
|
||||||
|
|
Loading…
Reference in New Issue