16 #ifndef INCLUDE_NN_NLIB_THREADING_THREAD_H_ 17 #define INCLUDE_NN_NLIB_THREADING_THREAD_H_ 20 #include "nn/nlib/Swap.h" 33 if (is_initialized_) {
43 NLIB_THREAD_ATTR_KEY_DETACHSTATE,
53 NLIB_THREAD_ATTR_KEY_DETACHSTATE,
57 return (detached != 0);
63 NLIB_THREAD_ATTR_KEY_STACKSIZE,
72 NLIB_THREAD_ATTR_KEY_STACKSIZE,
82 NLIB_THREAD_ATTR_KEY_PRIORITY,
91 NLIB_THREAD_ATTR_KEY_PRIORITY,
98 return is_initialized_ ? &attr_ :
nullptr;
101 return is_initialized_ ? &attr_ :
nullptr;
106 if (!is_initialized_) {
108 NLIB_ASSERT_NOERR(e);
110 is_initialized_ =
true;
113 mutable bool is_initialized_;
117 template <
class T1 = None,
class T2 = None,
class T3 = None,
class T4 = None,
class T5 = None>
121 typedef void (*Func)(ArgType& ptr);
124 : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_), arg4(arg4_), arg5(arg5_) {}
125 static void Call(ArgType& ptr) { ptr->func(ptr); }
136 struct ThreadArg<None, None, None, None, None> {
139 typedef void (*Func)();
143 struct ThreadArg<T1, None, None, None, None> {
146 typedef void (*Func)(ArgType& ptr);
148 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_) : func(func_), arg1(arg1_) {}
149 static void Call(ArgType& ptr) { ptr->func(ptr); }
155 template <
class T1,
class T2>
156 struct ThreadArg<T1, T2, None, None, None> {
159 typedef void (*Func)(ArgType& ptr);
161 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_) : func(func_), arg1(arg1_), arg2(arg2_) {}
162 static void Call(ArgType& ptr) { ptr->func(ptr); }
169 template <
class T1,
class T2,
class T3>
170 struct ThreadArg<T1, T2, T3, None, None> {
173 typedef void (*Func)(ArgType& ptr);
175 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_)
176 : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
177 static void Call(ArgType& ptr) { ptr->func(ptr); }
185 template <
class T1,
class T2,
class T3,
class T4>
189 typedef void (*Func)(ArgType& ptr);
191 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_)
192 : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_), arg4(arg4_) {}
193 static void Call(ArgType& ptr) { ptr->func(ptr); }
203 #pragma warning(push) 204 #pragma warning(disable : 4180) 214 typedef void (*ThreadFunc)(
void* arg);
219 #ifdef __cpp_rvalue_references 224 thread_id_ = rhs.thread_id_;
233 thread_id_ = rhs.thread_id_;
239 swap(thread_id_, rhs.thread_id_);
244 if (!thread_id_)
return ESRCH;
283 errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func), settings);
287 errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func));
290 template <
class ThArg>
293 if (!ptr)
return EINVAL;
294 errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
295 reinterpret_cast<void*>(ptr.get()), settings);
296 if (e == 0) ptr.release();
299 template <
class ThArg>
301 if (!ptr)
return EINVAL;
302 errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
303 reinterpret_cast<void*>(ptr.get()));
304 if (e == 0) ptr.release();
309 static void Exec(
void* p) {
314 static void Exec(
void* p) {
319 #define NLIB_THFUNC_USESWAP_NO(tp) \ 321 !IsSame<None, typename RemoveCv<B>::type>::value && \ 322 (IsSame<FalseType, typename RemoveCv<B>::type>::value || !IsSwappable<tp>::value), \ 324 #define NLIB_THFUNC_USESWAP_YES(tp) \ 325 typename EnableIf<!IsSame<None, typename RemoveCv<B>::type>::value && \ 326 IsSame<TrueType, typename RemoveCv<B>::type>::value && \ 327 IsSwappable<tp>::value, \ 331 struct ArgType :
public Conditional<
332 IsArithmetic<T>::value || IsPointer<T>::value || IsMemberPointer<T>::value,
333 FalseType, TrueType> {};
335 template <
class FUNC>
337 typedef typename RemoveRef<FUNC>::type FUNC_;
339 Args0(NLIB_THFUNC_USESWAP_NO(FUNC_) func_, B)
342 Args0(NLIB_THFUNC_USESWAP_YES(FUNC_) func_, B)
345 swap(const_cast<FUNC&>(func_), func);
348 Args0(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
357 template <
class FUNC,
class T1>
358 struct Args1 :
public Args0<FUNC> {
359 typedef Args0<FUNC> BaseType;
360 typedef typename RemoveRef<T1>::type T1_;
362 Args1(
const FUNC& func_, NLIB_THFUNC_USESWAP_NO(T1) arg1_, B)
363 : BaseType(func_,
typename IsSwappable<FUNC>::type()), arg1(arg1_) {}
365 Args1(
const FUNC& func_, NLIB_THFUNC_USESWAP_YES(T1) arg1_, B)
366 : BaseType(func_,
typename IsSwappable<FUNC>::type()), arg1() {
368 swap(const_cast<T1&>(arg1_), arg1);
371 Args1(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
373 const T1& arg1_, B b)
374 : BaseType(func_, b), arg1(arg1_) {}
377 template <
class FUNC,
class T1,
class T2>
378 struct Args2 :
public Args1<FUNC, T1> {
379 typedef Args1<FUNC, T1> BaseType;
380 typedef typename RemoveRef<T2>::type T2_;
382 Args2(
const FUNC& func_,
const T1& arg1_, NLIB_THFUNC_USESWAP_NO(T2) arg2_, B)
383 : BaseType(func_, arg1_,
typename ArgType<T1>::type()), arg2(arg2_) {}
385 Args2(
const FUNC& func_,
const T1& arg1_, NLIB_THFUNC_USESWAP_YES(T2) arg2_, B)
386 : BaseType(func_, arg1_,
typename ArgType<T1>::type()), arg2() {
388 swap(const_cast<T2&>(arg2_), arg2);
391 Args2(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
393 const T1& arg1_,
const T2& arg2_, B b)
394 : BaseType(func_, arg1_, b), arg2(arg2_) {}
397 template <
class FUNC,
class T1,
class T2,
class T3>
398 struct Args3 :
public Args2<FUNC, T1, T2> {
399 typedef Args2<FUNC, T1, T2> BaseType;
400 typedef typename RemoveRef<T3>::type T3_;
402 Args3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_, NLIB_THFUNC_USESWAP_NO(T3) arg3_,
404 : BaseType(func_, arg1_, arg2_,
typename ArgType<T2>::type()), arg3(arg3_) {}
406 Args3(
const FUNC& func,
const T1& arg1_,
const T2& arg2_, NLIB_THFUNC_USESWAP_YES(T3) arg3_,
408 : BaseType(func, arg1_, arg2_,
typename ArgType<T2>::type()), arg3() {
410 swap(const_cast<T3&>(arg3_), arg3);
413 Args3(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
415 const T1& arg1_,
const T2& arg2_,
const T3& arg3_, B b)
416 : BaseType(func_, arg1_, arg2_, b), arg3(arg3_) {}
419 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
420 struct Args4 :
public Args3<FUNC, T1, T2, T3> {
421 typedef Args3<FUNC, T1, T2, T3> BaseType;
422 typedef typename RemoveRef<T4>::type T4_;
424 Args4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
425 NLIB_THFUNC_USESWAP_NO(T4) arg4_, B)
426 : BaseType(func_, arg1_, arg2_, arg3_,
typename ArgType<T3>::type()), arg4(arg4_) {}
428 Args4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
429 NLIB_THFUNC_USESWAP_YES(T4) arg4_, B)
430 : BaseType(func_, arg1_, arg2_, arg3_,
typename ArgType<T3>::type()), arg4() {
432 swap(const_cast<T4&>(arg4_), arg4);
435 Args4(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
437 const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_, B b)
438 : BaseType(func_, arg1_, arg2_, arg3_, b), arg4(arg4_) {}
441 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
442 struct Args5 :
public Args4<FUNC, T1, T2, T3, T4> {
443 typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
444 typedef typename RemoveRef<T5>::type T5_;
446 Args5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
447 NLIB_THFUNC_USESWAP_NO(T5) arg5_, B)
448 : BaseType(func_, arg1_, arg2_, arg3_, arg4_,
typename ArgType<T4>::type()),
451 Args5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
452 NLIB_THFUNC_USESWAP_YES(T4) arg5_, B)
453 : BaseType(func_, arg1_, arg2_, arg3_, arg4_,
typename ArgType<T4>::type()), arg5() {
455 swap(const_cast<T5&>(arg5_), arg5);
458 Args5(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
460 const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
const T5& arg5_,
462 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b), arg5(arg5_) {}
465 #undef NLIB_THFUNC_USESWAP_NO 466 #undef NLIB_THFUNC_USESWAP_YES 468 template <
class FUNC>
469 struct ThreadFuncX0 :
public Args0<FUNC> {
470 typedef ThreadFuncX0<FUNC> ThisType;
471 typedef Args0<FUNC> BaseType;
473 ThreadFuncX0(
const FUNC& func_, B b)
474 : BaseType(func_, b) {}
475 explicit ThreadFuncX0(
const FUNC& func_) : BaseType(func_, None()) {}
480 template <
class FUNC,
class T1>
481 struct ThreadFuncX1 :
public Args1<FUNC, T1> {
482 typedef ThreadFuncX1<FUNC, T1> ThisType;
483 typedef Args1<FUNC, T1> BaseType;
485 ThreadFuncX1(
const FUNC& func_,
const T1& arg1_, B b)
486 : BaseType(func_, arg1_, b) {}
487 ThreadFuncX1(
const FUNC& func_,
const T1& arg1_) : BaseType(func_, arg1_, None()) {}
489 ptr->func(NLIB_MOVE(ptr->arg1));
492 template <
class FUNC,
class T1,
class T2>
493 struct ThreadFuncX2 :
public Args2<FUNC, T1, T2> {
494 typedef ThreadFuncX2<FUNC, T1, T2> ThisType;
495 typedef Args2<FUNC, T1, T2> BaseType;
497 ThreadFuncX2(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_, B b)
498 : BaseType(func_, arg1_, arg2_, b) {}
499 ThreadFuncX2(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_)
500 : BaseType(func_, arg1_, arg2_, None()) {}
502 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2));
505 template <
class FUNC,
class T1,
class T2,
class T3>
506 struct ThreadFuncX3 :
public Args3<FUNC, T1, T2, T3> {
507 typedef ThreadFuncX3<FUNC, T1, T2, T3> ThisType;
508 typedef Args3<FUNC, T1, T2, T3> BaseType;
510 ThreadFuncX3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_, B b)
511 : BaseType(func_, arg1_, arg2_, arg3_, b) {}
512 ThreadFuncX3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_)
513 : BaseType(func_, arg1_, arg2_, arg3_, None()) {}
515 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3));
518 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
519 struct ThreadFuncX4 :
public Args4<FUNC, T1, T2, T3, T4> {
520 typedef ThreadFuncX4<FUNC, T1, T2, T3, T4> ThisType;
521 typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
523 ThreadFuncX4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
524 const T4& arg4_, B b)
525 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b) {}
526 ThreadFuncX4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
528 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, None()) {}
530 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
531 NLIB_MOVE(ptr->arg4));
534 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
535 struct ThreadFuncX5 :
public Args5<FUNC, T1, T2, T3, T4, T5> {
536 typedef ThreadFuncX5<FUNC, T1, T2, T3, T4, T5> ThisType;
537 typedef Args5<FUNC, T1, T2, T3, T4, T5> BaseType;
539 ThreadFuncX5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
540 const T4& arg4_,
const T5& arg5_, B b)
541 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, b) {}
542 ThreadFuncX5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
543 const T4& arg4_,
const T5& arg5_)
544 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, None()) {}
546 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
547 NLIB_MOVE(ptr->arg4), NLIB_MOVE(ptr->arg5));
552 template <
class FUNC>
555 new (std::nothrow) ThreadFuncX0<FUNC>(f,
typename IsSwappable<FUNC>::type()));
556 if (!ptr)
return ENOMEM;
557 return this->StartRaw(settings, ptr);
559 template <
class FUNC>
562 new (std::nothrow) ThreadFuncX0<FUNC>(f,
typename IsSwappable<FUNC>::type()));
563 if (!ptr)
return ENOMEM;
564 return this->StartRaw(ptr);
566 template <
class FUNC,
class T1>
569 new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1,
typename ArgType<T1>::type()));
570 if (!ptr)
return ENOMEM;
571 return this->StartRaw(settings, ptr);
573 template <
class FUNC,
class T1>
576 new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1,
typename ArgType<T1>::type()));
577 if (!ptr)
return ENOMEM;
578 return this->StartRaw(ptr);
580 template <
class FUNC,
class T1,
class T2>
584 f, arg1, arg2,
typename ArgType<T2>::type()));
585 if (!ptr)
return ENOMEM;
586 return this->StartRaw(settings, ptr);
588 template <
class FUNC,
class T1,
class T2>
591 f, arg1, arg2,
typename ArgType<T2>::type()));
592 if (!ptr)
return ENOMEM;
593 return this->StartRaw(ptr);
595 template <
class FUNC,
class T1,
class T2,
class T3>
600 ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3,
typename ArgType<T3>::type()));
601 if (!ptr)
return ENOMEM;
602 return this->StartRaw(settings, ptr);
604 template <
class FUNC,
class T1,
class T2,
class T3>
608 ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3,
typename ArgType<T3>::type()));
609 if (!ptr)
return ENOMEM;
610 return this->StartRaw(ptr);
612 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
614 const T3& arg3,
const T4& arg4,
move_tag) {
616 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
617 typename ArgType<T4>::type()));
618 if (!ptr)
return ENOMEM;
619 return this->StartRaw(settings, ptr);
621 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
622 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
625 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
626 typename ArgType<T4>::type()));
627 if (!ptr)
return ENOMEM;
628 return this->StartRaw(ptr);
630 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
632 const T3& arg3,
const T4& arg4,
const T5& arg5,
move_tag) {
634 new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
635 f, arg1, arg2, arg3, arg4, arg5,
typename ArgType<T5>::type()));
636 if (!ptr)
return ENOMEM;
637 return this->StartRaw(settings, ptr);
639 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
640 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
643 new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
644 f, arg1, arg2, arg3, arg4, arg5,
typename ArgType<T5>::type()));
645 if (!ptr)
return ENOMEM;
646 return this->StartRaw(ptr);
648 template <
class FUNC>
651 if (!ptr)
return ENOMEM;
652 return this->StartRaw(settings, ptr);
654 template <
class FUNC>
657 if (!ptr)
return ENOMEM;
658 return this->StartRaw(ptr);
660 template <
class FUNC,
class T1>
663 if (!ptr)
return ENOMEM;
664 return this->StartRaw(settings, ptr);
666 template <
class FUNC,
class T1>
669 if (!ptr)
return ENOMEM;
670 return this->StartRaw(ptr);
672 template <
class FUNC,
class T1,
class T2>
675 ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
676 if (!ptr)
return ENOMEM;
677 return this->StartRaw(settings, ptr);
679 template <
class FUNC,
class T1,
class T2>
682 ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
683 if (!ptr)
return ENOMEM;
684 return this->StartRaw(ptr);
686 template <
class FUNC,
class T1,
class T2,
class T3>
690 new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
691 if (!ptr)
return ENOMEM;
692 return this->StartRaw(settings, ptr);
694 template <
class FUNC,
class T1,
class T2,
class T3>
695 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3) {
697 new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
698 if (!ptr)
return ENOMEM;
699 return this->StartRaw(ptr);
701 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
703 const T3& arg3,
const T4& arg4) {
705 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
706 if (!ptr)
return ENOMEM;
707 return this->StartRaw(settings, ptr);
709 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
710 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4) {
712 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
713 if (!ptr)
return ENOMEM;
714 return this->StartRaw(ptr);
716 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
718 const T3& arg3,
const T4& arg4,
const T5& arg5) {
720 std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
721 if (!ptr)
return ENOMEM;
722 return this->StartRaw(settings, ptr);
724 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
725 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
728 std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
729 if (!ptr)
return ENOMEM;
730 return this->StartRaw(ptr);
743 struct Thread::Args0<void()> {
745 Args0(
void (*func_)(), X)
747 Args0(
void (*func_)()) : func(func_) {}
755 struct Thread::Args0<void(T1)> {
757 Args0(
void (*func_)(T1), X)
759 Args0(
void (*func_)(T1)) : func(func_) {}
766 template <
class T1,
class T2>
767 struct Thread::Args0<void(T1, T2)> {
769 Args0(
void (*func_)(T1, T2), X)
771 Args0(
void (*func_)(T1, T2)) : func(func_) {}
772 void (*func)(T1, T2);
778 template <
class T1,
class T2,
class T3>
779 struct Thread::Args0<void(T1, T2, T3)> {
781 Args0(
void (*func_)(T1, T2, T3), X)
783 Args0(
void (*func_)(T1, T2, T3)) : func(func_) {}
784 void (*func)(T1, T2, T3);
790 template <
class T1,
class T2,
class T3,
class T4>
791 struct Thread::Args0<void(T1, T2, T3, T4)> {
793 Args0(
void (*func_)(T1, T2, T3, T4), X)
795 Args0(
void (*func_)(T1, T2, T3, T4)) : func(func_) {}
796 void (*func)(T1, T2, T3, T4);
802 template <
class T1,
class T2,
class T3,
class T4,
class T5>
803 struct Thread::Args0<void(T1, T2, T3, T4, T5)> {
805 Args0(
void (*func_)(T1, T2, T3, T4, T5), X)
807 Args0(
void (*func_)(T1, T2, T3, T4, T5)) : func(func_) {}
808 void (*func)(T1, T2, T3, T4, T5);
815 unsigned int num_cpu;
820 namespace this_thread {
831 return e == 0 ? id : -1;
837 if (e != 0)
return e;
848 NLIB_DEFINE_STD_SWAP(::nlib_ns::threading::Thread)
850 #endif // INCLUDE_NN_NLIB_THREADING_THREAD_H_ errno_t GetCpu(int *cpuid) noexcept
Gets the CPU on which the called thread is executing.
errno_t YieldThread() noexcept
Yields control to a different thread.
errno_t Start(const ThreadSettings &settings, const FUNC &f, move_tag)
This function is nearly identical to the version without start options, except that it can specify th...
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
Starts a function that has two arguments in a different thread. This function is nearly identical to ...
nlib_thread GetNativeHandle() const noexcept
Gets the implementation dependent thread ID.
static errno_t Sleep(const TimeSpan &span) noexcept
Sleeps only for the duration of span.
constexpr Thread() noexcept
Instantiates the object with default parameters (default constructor).
int GetStackSize() const noexcept
Returns the set stack size.
errno_t Sleep(const TimeSpan &span) noexcept
Makes the thread sleep for a specified period of time.
constexpr ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_)
This constructor initializes the data structure field.
Substitute definitions for the C++11 standard header type_traits. These substitute definitions are us...
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
This function is nearly identical to the version without start options, except that it can specify th...
bool operator!=(const Thread &rhs) noexcept
Compares threads using the nlib_thread_equal function.
constexpr ThreadArg()
Instantiates the object with default parameters (default constructor).
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
T1 arg1
Object passed to a function being run in a different thread.
errno_t Detach() noexcept
Detaches the thread.
Func func
Pointer to a function being run in a different thread.
bool GetDetachState() const noexcept
Gets whether the thread is set to start in a detached state.
errno_t GetPriority(int32_t *priority) noexcept
Gets the thread priority.
T3 arg3
Object passed to a function being run in a different thread.
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, move_tag)
Starts a function that has five arguments in a different thread. This function is nearly identical to...
~Thread() noexcept
Destructor. Joins if the thread has not been joined.
errno_t ChangePriority(int32_t priority) noexcept
Sets the thread priority.
T5 arg5
Object passed to a function being run in a different thread.
void SetDetachState(bool detached) noexcept
Sets whether to start a thread in a detached state.
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2)
Starts a function that has two arguments in a different thread. This function is nearly identical to ...
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
Starts a function that has three arguments in a different thread. This function is nearly identical t...
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
Starts a function that has five arguments in a different thread. This function is nearly identical to...
Defines that class that is corresponding to std::unique_ptr.
Class to create and start threads.
Structure to run a thread in a Thread class.
void swap(Thread &rhs) noexcept
Swaps the contents of an object.
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
Starts a function that has three arguments in a different thread. This function is nearly identical t...
int GetPriority() const noexcept
Gets the thread priority.
bool IsJoinable() const noexcept
Checks whether a join is possible.
Thread(Thread &rhs, move_tag) noexcept
Corresponds to a move constructor.
errno_t SetStackSize(int size) noexcept
Sets the stack size.
errno_t Start(const FUNC &f, const T1 &arg1, move_tag)
Starts a function that has one argument in a different thread.
errno_t StartRaw(UniquePtr< ThArg > &ptr) noexcept
Starts running another thread.
errno_t SetName(const char *literal_string) noexcept
Attaches a name to the thread.
Class to wrap nlib_thread_attr. nlib_thread_attr_init() and nlib_thread_attr_destroy() are run automa...
errno_t StartRaw(const ThreadSettings &settings, UniquePtr< ThArg > &ptr) noexcept
Starts running another thread.
Thread & assign(Thread &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
static errno_t YieldThread() noexcept
Calls the nlib_yield function.
errno_t Join() noexcept
Waits for a thread to complete execution.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
This function is nearly identical to the version without start options, except that it can specify th...
An empty structure indicating that an argument to a function needs to be moved.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1)
This function is nearly identical to the version without start options, except that it can specify th...
bool operator==(const Thread &rhs) noexcept
Compares threads using the nlib_thread_equal function.
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
Starts a function that has four arguments in a different thread. This function is nearly identical to...
errno_t Start(const FUNC &f, const T1 &arg1)
Starts a function that has one argument in a different thread.
errno_t Start(const ThreadSettings &settings, const FUNC &f)
This function is nearly identical to the version without start options, except that it can specify th...
errno_t SetPriority(int priority) noexcept
Sets the thread priority. The priority value is platform dependent.
Defines the class for handling times and durations.
nlib_thread_id GetId() noexcept
Gets the current thread ID.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
A file that contains the configuration information for each development environment.
Thread & operator=(Thread &&rhs) noexcept
Move assignment operator. This function is useful when using C++11.
errno_t Start(const FUNC &f)
Starts a function that has no arguments in a different thread.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
This function is nearly identical to the version without start options, except that it can specify th...
size_t GetHardwareConcurrency() noexcept
Returns the number of hardware threads.
errno_t SetAffinity(uint32_t affinity) noexcept
Sets the processor affinity mask for the thread.
ThreadArg< T1, T2, T3, T4, T5 > ThisType
typedef for this data structure type.
errno_t StartRaw(const ThreadSettings &settings, ThreadArg<>::Func func) noexcept
Starts a function that has no arguments in a different thread.
UniquePtr< ThisType > ArgType
typedef for the thread function argument type.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
This function is nearly identical to the version without start options, except that it can specify th...
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Thread(Thread &&rhs) noexcept
Instantiates the object (move constructor). This function is useful when using C++11.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
This function is nearly identical to the version without start options, except that it can specify th...
The class for representing the time.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
This function is nearly identical to the version without start options, except that it can specify th...
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, move_tag)
This function is nearly identical to the version without start options, except that it can specify th...
errno_t SleepMilliSeconds(unsigned int millisec) noexcept
Makes the thread sleep for a specified period of time.
errno_t StartRaw(ThreadArg<>::Func func) noexcept
Starts a function that has no arguments in a different thread.
T4 arg4
Object passed to a function being run in a different thread.
errno_t GetPriority(int32_t *priority) noexcept
Calls nlib_thread_getpriority and gets the thread's execution priority.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, move_tag)
This function is nearly identical to the version without start options, except that it can specify th...
T2 arg2
Object passed to a function being run in a different thread.
errno_t Start(const FUNC &f, move_tag)
Starts a function that has no arguments in a different thread. This function is nearly identical to t...
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
Starts a function that has four arguments in a different thread. This function is nearly identical to...
errno_t ChangePriority(int32_t priority) noexcept
Calls nlib_thread_setpriority and sets the thread's execution priority.
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2)
This function is nearly identical to the version without start options, except that it can specify th...