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_ : NULL;
101 return is_initialized_ ? &attr_ : NULL;
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 NLIB_MOVE_MEMBER_HELPER_1(
Thread, thread_id_)
223 if (!thread_id_)
return ESRCH;
250 swap(thread_id_, rhs.thread_id_);
266 errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func), settings);
270 errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func));
273 template <
class ThArg>
276 if (!ptr)
return EINVAL;
277 errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
278 reinterpret_cast<void*>(ptr.get()), settings);
279 if (e == 0) ptr.release();
282 template <
class ThArg>
284 if (!ptr)
return EINVAL;
285 errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
286 reinterpret_cast<void*>(ptr.get()));
287 if (e == 0) ptr.release();
292 static void Exec(
void* p) {
297 static void Exec(
void* p) {
302 #define NLIB_THFUNC_USESWAP_NO(tp) \ 304 !IsSame<None, typename RemoveCv<B>::type>::value && \ 305 (IsSame<FalseType, typename RemoveCv<B>::type>::value || !IsSwappable<tp>::value), \ 307 #define NLIB_THFUNC_USESWAP_YES(tp) \ 308 typename EnableIf<!IsSame<None, typename RemoveCv<B>::type>::value && \ 309 IsSame<TrueType, typename RemoveCv<B>::type>::value && \ 310 IsSwappable<tp>::value, \ 314 struct ArgType :
public Conditional<
315 IsArithmetic<T>::value || IsPointer<T>::value || IsMemberPointer<T>::value,
316 FalseType, TrueType> {};
318 template <
class FUNC>
320 typedef typename RemoveRef<FUNC>::type FUNC_;
322 Args0(NLIB_THFUNC_USESWAP_NO(FUNC_) func_, B)
325 Args0(NLIB_THFUNC_USESWAP_YES(FUNC_) func_, B)
328 swap(const_cast<FUNC&>(func_), func);
331 Args0(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
340 template <
class FUNC,
class T1>
341 struct Args1 :
public Args0<FUNC> {
342 typedef Args0<FUNC> BaseType;
343 typedef typename RemoveRef<T1>::type T1_;
345 Args1(
const FUNC& func_, NLIB_THFUNC_USESWAP_NO(T1) arg1_, B)
346 : BaseType(func_,
typename IsSwappable<FUNC>::type()), arg1(arg1_) {}
348 Args1(
const FUNC& func_, NLIB_THFUNC_USESWAP_YES(T1) arg1_, B)
349 : BaseType(func_,
typename IsSwappable<FUNC>::type()), arg1() {
351 swap(const_cast<T1&>(arg1_), arg1);
354 Args1(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
356 const T1& arg1_, B b)
357 : BaseType(func_, b), arg1(arg1_) {}
360 template <
class FUNC,
class T1,
class T2>
361 struct Args2 :
public Args1<FUNC, T1> {
362 typedef Args1<FUNC, T1> BaseType;
363 typedef typename RemoveRef<T2>::type T2_;
365 Args2(
const FUNC& func_,
const T1& arg1_, NLIB_THFUNC_USESWAP_NO(T2) arg2_, B)
366 : BaseType(func_, arg1_,
typename ArgType<T1>::type()), arg2(arg2_) {}
368 Args2(
const FUNC& func_,
const T1& arg1_, NLIB_THFUNC_USESWAP_YES(T2) arg2_, B)
369 : BaseType(func_, arg1_,
typename ArgType<T1>::type()), arg2() {
371 swap(const_cast<T2&>(arg2_), arg2);
374 Args2(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
376 const T1& arg1_,
const T2& arg2_, B b)
377 : BaseType(func_, arg1_, b), arg2(arg2_) {}
380 template <
class FUNC,
class T1,
class T2,
class T3>
381 struct Args3 :
public Args2<FUNC, T1, T2> {
382 typedef Args2<FUNC, T1, T2> BaseType;
383 typedef typename RemoveRef<T3>::type T3_;
385 Args3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_, NLIB_THFUNC_USESWAP_NO(T3) arg3_,
387 : BaseType(func_, arg1_, arg2_,
typename ArgType<T2>::type()), arg3(arg3_) {}
389 Args3(
const FUNC& func,
const T1& arg1_,
const T2& arg2_, NLIB_THFUNC_USESWAP_YES(T3) arg3_,
391 : BaseType(func, arg1_, arg2_,
typename ArgType<T2>::type()), arg3() {
393 swap(const_cast<T3&>(arg3_), arg3);
396 Args3(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
398 const T1& arg1_,
const T2& arg2_,
const T3& arg3_, B b)
399 : BaseType(func_, arg1_, arg2_, b), arg3(arg3_) {}
402 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
403 struct Args4 :
public Args3<FUNC, T1, T2, T3> {
404 typedef Args3<FUNC, T1, T2, T3> BaseType;
405 typedef typename RemoveRef<T4>::type T4_;
407 Args4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
408 NLIB_THFUNC_USESWAP_NO(T4) arg4_, B)
409 : BaseType(func_, arg1_, arg2_, arg3_,
typename ArgType<T3>::type()), arg4(arg4_) {}
411 Args4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
412 NLIB_THFUNC_USESWAP_YES(T4) arg4_, B)
413 : BaseType(func_, arg1_, arg2_, arg3_,
typename ArgType<T3>::type()), arg4() {
415 swap(const_cast<T4&>(arg4_), arg4);
418 Args4(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
420 const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_, B b)
421 : BaseType(func_, arg1_, arg2_, arg3_, b), arg4(arg4_) {}
424 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
425 struct Args5 :
public Args4<FUNC, T1, T2, T3, T4> {
426 typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
427 typedef typename RemoveRef<T5>::type T5_;
429 Args5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
430 NLIB_THFUNC_USESWAP_NO(T5) arg5_, B)
431 : BaseType(func_, arg1_, arg2_, arg3_, arg4_,
typename ArgType<T4>::type()),
434 Args5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
435 NLIB_THFUNC_USESWAP_YES(T4) arg5_, B)
436 : BaseType(func_, arg1_, arg2_, arg3_, arg4_,
typename ArgType<T4>::type()), arg5() {
438 swap(const_cast<T5&>(arg5_), arg5);
441 Args5(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
443 const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
const T5& arg5_,
445 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b), arg5(arg5_) {}
448 #undef NLIB_THFUNC_USESWAP_NO 449 #undef NLIB_THFUNC_USESWAP_YES 451 template <
class FUNC>
452 struct ThreadFuncX0 :
public Args0<FUNC> {
453 typedef ThreadFuncX0<FUNC> ThisType;
454 typedef Args0<FUNC> BaseType;
456 ThreadFuncX0(
const FUNC& func_, B b)
457 : BaseType(func_, b) {}
458 explicit ThreadFuncX0(
const FUNC& func_) : BaseType(func_, None()) {}
463 template <
class FUNC,
class T1>
464 struct ThreadFuncX1 :
public Args1<FUNC, T1> {
465 typedef ThreadFuncX1<FUNC, T1> ThisType;
466 typedef Args1<FUNC, T1> BaseType;
468 ThreadFuncX1(
const FUNC& func_,
const T1& arg1_, B b)
469 : BaseType(func_, arg1_, b) {}
470 ThreadFuncX1(
const FUNC& func_,
const T1& arg1_) : BaseType(func_, arg1_, None()) {}
472 ptr->func(NLIB_MOVE(ptr->arg1));
475 template <
class FUNC,
class T1,
class T2>
476 struct ThreadFuncX2 :
public Args2<FUNC, T1, T2> {
477 typedef ThreadFuncX2<FUNC, T1, T2> ThisType;
478 typedef Args2<FUNC, T1, T2> BaseType;
480 ThreadFuncX2(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_, B b)
481 : BaseType(func_, arg1_, arg2_, b) {}
482 ThreadFuncX2(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_)
483 : BaseType(func_, arg1_, arg2_, None()) {}
485 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2));
488 template <
class FUNC,
class T1,
class T2,
class T3>
489 struct ThreadFuncX3 :
public Args3<FUNC, T1, T2, T3> {
490 typedef ThreadFuncX3<FUNC, T1, T2, T3> ThisType;
491 typedef Args3<FUNC, T1, T2, T3> BaseType;
493 ThreadFuncX3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_, B b)
494 : BaseType(func_, arg1_, arg2_, arg3_, b) {}
495 ThreadFuncX3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_)
496 : BaseType(func_, arg1_, arg2_, arg3_, None()) {}
498 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3));
501 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
502 struct ThreadFuncX4 :
public Args4<FUNC, T1, T2, T3, T4> {
503 typedef ThreadFuncX4<FUNC, T1, T2, T3, T4> ThisType;
504 typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
506 ThreadFuncX4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
507 const T4& arg4_, B b)
508 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b) {}
509 ThreadFuncX4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
511 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, None()) {}
513 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
514 NLIB_MOVE(ptr->arg4));
517 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
518 struct ThreadFuncX5 :
public Args5<FUNC, T1, T2, T3, T4, T5> {
519 typedef ThreadFuncX5<FUNC, T1, T2, T3, T4, T5> ThisType;
520 typedef Args5<FUNC, T1, T2, T3, T4, T5> BaseType;
522 ThreadFuncX5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
523 const T4& arg4_,
const T5& arg5_, B b)
524 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, b) {}
525 ThreadFuncX5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
526 const T4& arg4_,
const T5& arg5_)
527 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, None()) {}
529 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
530 NLIB_MOVE(ptr->arg4), NLIB_MOVE(ptr->arg5));
535 template <
class FUNC>
538 new (std::nothrow) ThreadFuncX0<FUNC>(f,
typename IsSwappable<FUNC>::type()));
539 if (!ptr)
return ENOMEM;
540 return this->StartRaw(settings, ptr);
542 template <
class FUNC>
545 new (std::nothrow) ThreadFuncX0<FUNC>(f,
typename IsSwappable<FUNC>::type()));
546 if (!ptr)
return ENOMEM;
547 return this->StartRaw(ptr);
549 template <
class FUNC,
class T1>
552 new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1,
typename ArgType<T1>::type()));
553 if (!ptr)
return ENOMEM;
554 return this->StartRaw(settings, ptr);
556 template <
class FUNC,
class T1>
559 new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1,
typename ArgType<T1>::type()));
560 if (!ptr)
return ENOMEM;
561 return this->StartRaw(ptr);
563 template <
class FUNC,
class T1,
class T2>
567 f, arg1, arg2,
typename ArgType<T2>::type()));
568 if (!ptr)
return ENOMEM;
569 return this->StartRaw(settings, ptr);
571 template <
class FUNC,
class T1,
class T2>
574 f, arg1, arg2,
typename ArgType<T2>::type()));
575 if (!ptr)
return ENOMEM;
576 return this->StartRaw(ptr);
578 template <
class FUNC,
class T1,
class T2,
class T3>
583 ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3,
typename ArgType<T3>::type()));
584 if (!ptr)
return ENOMEM;
585 return this->StartRaw(settings, ptr);
587 template <
class FUNC,
class T1,
class T2,
class T3>
591 ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3,
typename ArgType<T3>::type()));
592 if (!ptr)
return ENOMEM;
593 return this->StartRaw(ptr);
595 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
597 const T3& arg3,
const T4& arg4,
move_tag) {
599 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
600 typename ArgType<T4>::type()));
601 if (!ptr)
return ENOMEM;
602 return this->StartRaw(settings, ptr);
604 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
605 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
608 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
609 typename ArgType<T4>::type()));
610 if (!ptr)
return ENOMEM;
611 return this->StartRaw(ptr);
613 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
615 const T3& arg3,
const T4& arg4,
const T5& arg5,
move_tag) {
617 new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
618 f, arg1, arg2, arg3, arg4, arg5,
typename ArgType<T5>::type()));
619 if (!ptr)
return ENOMEM;
620 return this->StartRaw(settings, ptr);
622 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
623 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
626 new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
627 f, arg1, arg2, arg3, arg4, arg5,
typename ArgType<T5>::type()));
628 if (!ptr)
return ENOMEM;
629 return this->StartRaw(ptr);
631 template <
class FUNC>
634 if (!ptr)
return ENOMEM;
635 return this->StartRaw(settings, ptr);
637 template <
class FUNC>
640 if (!ptr)
return ENOMEM;
641 return this->StartRaw(ptr);
643 template <
class FUNC,
class T1>
646 if (!ptr)
return ENOMEM;
647 return this->StartRaw(settings, ptr);
649 template <
class FUNC,
class T1>
652 if (!ptr)
return ENOMEM;
653 return this->StartRaw(ptr);
655 template <
class FUNC,
class T1,
class T2>
658 ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
659 if (!ptr)
return ENOMEM;
660 return this->StartRaw(settings, ptr);
662 template <
class FUNC,
class T1,
class T2>
665 ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
666 if (!ptr)
return ENOMEM;
667 return this->StartRaw(ptr);
669 template <
class FUNC,
class T1,
class T2,
class T3>
673 new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
674 if (!ptr)
return ENOMEM;
675 return this->StartRaw(settings, ptr);
677 template <
class FUNC,
class T1,
class T2,
class T3>
678 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3) {
680 new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
681 if (!ptr)
return ENOMEM;
682 return this->StartRaw(ptr);
684 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
686 const T3& arg3,
const T4& arg4) {
688 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
689 if (!ptr)
return ENOMEM;
690 return this->StartRaw(settings, ptr);
692 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
693 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4) {
695 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
696 if (!ptr)
return ENOMEM;
697 return this->StartRaw(ptr);
699 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
701 const T3& arg3,
const T4& arg4,
const T5& arg5) {
703 std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
704 if (!ptr)
return ENOMEM;
705 return this->StartRaw(settings, ptr);
707 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
708 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
711 std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
712 if (!ptr)
return ENOMEM;
713 return this->StartRaw(ptr);
726 struct Thread::Args0<void()> {
728 Args0(
void (*func_)(), X)
730 Args0(
void (*func_)()) : func(func_) {}
738 struct Thread::Args0<void(T1)> {
740 Args0(
void (*func_)(T1), X)
742 Args0(
void (*func_)(T1)) : func(func_) {}
749 template <
class T1,
class T2>
750 struct Thread::Args0<void(T1, T2)> {
752 Args0(
void (*func_)(T1, T2), X)
754 Args0(
void (*func_)(T1, T2)) : func(func_) {}
755 void (*func)(T1, T2);
761 template <
class T1,
class T2,
class T3>
762 struct Thread::Args0<void(T1, T2, T3)> {
764 Args0(
void (*func_)(T1, T2, T3), X)
766 Args0(
void (*func_)(T1, T2, T3)) : func(func_) {}
767 void (*func)(T1, T2, T3);
773 template <
class T1,
class T2,
class T3,
class T4>
774 struct Thread::Args0<void(T1, T2, T3, T4)> {
776 Args0(
void (*func_)(T1, T2, T3, T4), X)
778 Args0(
void (*func_)(T1, T2, T3, T4)) : func(func_) {}
779 void (*func)(T1, T2, T3, T4);
785 template <
class T1,
class T2,
class T3,
class T4,
class T5>
786 struct Thread::Args0<void(T1, T2, T3, T4, T5)> {
788 Args0(
void (*func_)(T1, T2, T3, T4, T5), X)
790 Args0(
void (*func_)(T1, T2, T3, T4, T5)) : func(func_) {}
791 void (*func)(T1, T2, T3, T4, T5);
798 unsigned int num_cpu;
803 namespace this_thread {
814 return e == 0 ? id : -1;
820 if (e != 0)
return e;
831 NLIB_DEFINE_STD_SWAP(::nlib_ns::threading::Thread)
833 #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.
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.
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.
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.
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...