3 #ifndef INCLUDE_NN_NLIB_THREADING_THREAD_H_ 4 #define INCLUDE_NN_NLIB_THREADING_THREAD_H_ 7 #include "nn/nlib/Swap.h" 20 if (is_initialized_) {
30 NLIB_THREAD_ATTR_KEY_DETACHSTATE,
40 NLIB_THREAD_ATTR_KEY_DETACHSTATE,
44 return (detached != 0);
50 NLIB_THREAD_ATTR_KEY_STACKSIZE,
59 NLIB_THREAD_ATTR_KEY_STACKSIZE,
69 NLIB_THREAD_ATTR_KEY_PRIORITY,
78 NLIB_THREAD_ATTR_KEY_PRIORITY,
85 return is_initialized_ ? &attr_ : NULL;
88 return is_initialized_ ? &attr_ : NULL;
93 if (!is_initialized_) {
97 is_initialized_ =
true;
100 mutable bool is_initialized_;
104 template <
class T1 = None,
class T2 = None,
class T3 = None,
class T4 = None,
class T5 = None>
108 typedef void (*Func)(ArgType& ptr);
111 : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_), arg4(arg4_), arg5(arg5_) {}
112 static void Call(ArgType& ptr) { ptr->func(ptr); }
123 struct ThreadArg<None, None, None, None, None> {
126 typedef void (*Func)();
130 struct ThreadArg<T1, None, None, None, None> {
133 typedef void (*Func)(ArgType& ptr);
135 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_) : func(func_), arg1(arg1_) {}
136 static void Call(ArgType& ptr) { ptr->func(ptr); }
142 template <
class T1,
class T2>
143 struct ThreadArg<T1, T2, None, None, None> {
146 typedef void (*Func)(ArgType& ptr);
148 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_) : func(func_), arg1(arg1_), arg2(arg2_) {}
149 static void Call(ArgType& ptr) { ptr->func(ptr); }
156 template <
class T1,
class T2,
class T3>
157 struct ThreadArg<T1, T2, T3, None, None> {
160 typedef void (*Func)(ArgType& ptr);
162 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_)
163 : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
164 static void Call(ArgType& ptr) { ptr->func(ptr); }
172 template <
class T1,
class T2,
class T3,
class T4>
176 typedef void (*Func)(ArgType& ptr);
178 NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_)
179 : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_), arg4(arg4_) {}
180 static void Call(ArgType& ptr) { ptr->func(ptr); }
190 #pragma warning(push) 191 #pragma warning(disable : 4180) 201 typedef void (*ThreadFunc)(
void* arg);
206 NLIB_MOVE_MEMBER_HELPER_1(
Thread, thread_id_)
210 if (!thread_id_)
return ESRCH;
237 swap(thread_id_, rhs.thread_id_);
253 errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func), settings);
257 errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func));
260 template <
class ThArg>
263 if (!ptr)
return EINVAL;
264 errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
265 reinterpret_cast<void*>(ptr.get()), settings);
266 if (e == 0) ptr.release();
269 template <
class ThArg>
271 if (!ptr)
return EINVAL;
272 errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
273 reinterpret_cast<void*>(ptr.get()));
274 if (e == 0) ptr.release();
279 static void Exec(
void* p) {
284 static void Exec(
void* p) {
289 #define NLIB_THFUNC_USESWAP_NO(tp) \ 291 !IsSame<None, typename RemoveCv<B>::type>::value && \ 292 (IsSame<FalseType, typename RemoveCv<B>::type>::value || !IsSwappable<tp>::value), \ 294 #define NLIB_THFUNC_USESWAP_YES(tp) \ 295 typename EnableIf<!IsSame<None, typename RemoveCv<B>::type>::value && \ 296 IsSame<TrueType, typename RemoveCv<B>::type>::value && \ 297 IsSwappable<tp>::value, \ 301 struct ArgType :
public Conditional<
302 IsArithmetic<T>::value || IsPointer<T>::value || IsMemberPointer<T>::value,
303 FalseType, TrueType> {};
305 template <
class FUNC>
307 typedef typename RemoveRef<FUNC>::type FUNC_;
309 Args0(NLIB_THFUNC_USESWAP_NO(FUNC_) func_, B)
312 Args0(NLIB_THFUNC_USESWAP_YES(FUNC_) func_, B)
315 swap(const_cast<FUNC&>(func_), func);
318 Args0(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
327 template <
class FUNC,
class T1>
328 struct Args1 :
public Args0<FUNC> {
329 typedef Args0<FUNC> BaseType;
330 typedef typename RemoveRef<T1>::type T1_;
332 Args1(
const FUNC& func_, NLIB_THFUNC_USESWAP_NO(T1) arg1_, B)
333 : BaseType(func_,
typename IsSwappable<FUNC>::type()), arg1(arg1_) {}
335 Args1(
const FUNC& func_, NLIB_THFUNC_USESWAP_YES(T1) arg1_, B)
336 : BaseType(func_,
typename IsSwappable<FUNC>::type()), arg1() {
338 swap(const_cast<T1&>(arg1_), arg1);
341 Args1(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
343 const T1& arg1_, B b)
344 : BaseType(func_, b), arg1(arg1_) {}
347 template <
class FUNC,
class T1,
class T2>
348 struct Args2 :
public Args1<FUNC, T1> {
349 typedef Args1<FUNC, T1> BaseType;
350 typedef typename RemoveRef<T2>::type T2_;
352 Args2(
const FUNC& func_,
const T1& arg1_, NLIB_THFUNC_USESWAP_NO(T2) arg2_, B)
353 : BaseType(func_, arg1_,
typename ArgType<T1>::type()), arg2(arg2_) {}
355 Args2(
const FUNC& func_,
const T1& arg1_, NLIB_THFUNC_USESWAP_YES(T2) arg2_, B)
356 : BaseType(func_, arg1_,
typename ArgType<T1>::type()), arg2() {
358 swap(const_cast<T2&>(arg2_), arg2);
361 Args2(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
363 const T1& arg1_,
const T2& arg2_, B b)
364 : BaseType(func_, arg1_, b), arg2(arg2_) {}
367 template <
class FUNC,
class T1,
class T2,
class T3>
368 struct Args3 :
public Args2<FUNC, T1, T2> {
369 typedef Args2<FUNC, T1, T2> BaseType;
370 typedef typename RemoveRef<T3>::type T3_;
372 Args3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_, NLIB_THFUNC_USESWAP_NO(T3) arg3_,
374 : BaseType(func_, arg1_, arg2_,
typename ArgType<T2>::type()), arg3(arg3_) {}
376 Args3(
const FUNC& func,
const T1& arg1_,
const T2& arg2_, NLIB_THFUNC_USESWAP_YES(T3) arg3_,
378 : BaseType(func, arg1_, arg2_,
typename ArgType<T2>::type()), arg3() {
380 swap(const_cast<T3&>(arg3_), arg3);
383 Args3(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
385 const T1& arg1_,
const T2& arg2_,
const T3& arg3_, B b)
386 : BaseType(func_, arg1_, arg2_, b), arg3(arg3_) {}
389 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
390 struct Args4 :
public Args3<FUNC, T1, T2, T3> {
391 typedef Args3<FUNC, T1, T2, T3> BaseType;
392 typedef typename RemoveRef<T4>::type T4_;
394 Args4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
395 NLIB_THFUNC_USESWAP_NO(T4) arg4_, B)
396 : BaseType(func_, arg1_, arg2_, arg3_,
typename ArgType<T3>::type()), arg4(arg4_) {}
398 Args4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
399 NLIB_THFUNC_USESWAP_YES(T4) arg4_, B)
400 : BaseType(func_, arg1_, arg2_, arg3_,
typename ArgType<T3>::type()), arg4() {
402 swap(const_cast<T4&>(arg4_), arg4);
405 Args4(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
407 const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_, B b)
408 : BaseType(func_, arg1_, arg2_, arg3_, b), arg4(arg4_) {}
411 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
412 struct Args5 :
public Args4<FUNC, T1, T2, T3, T4> {
413 typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
414 typedef typename RemoveRef<T5>::type T5_;
416 Args5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
417 NLIB_THFUNC_USESWAP_NO(T5) arg5_, B)
418 : BaseType(func_, arg1_, arg2_, arg3_, arg4_,
typename ArgType<T4>::type()),
421 Args5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
422 NLIB_THFUNC_USESWAP_YES(T4) arg5_, B)
423 : BaseType(func_, arg1_, arg2_, arg3_, arg4_,
typename ArgType<T4>::type()), arg5() {
425 swap(const_cast<T5&>(arg5_), arg5);
428 Args5(
typename EnableIf<IsSame<
typename RemoveCv<B>::type, None>::value,
const FUNC&>::type
430 const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
const T4& arg4_,
const T5& arg5_,
432 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b), arg5(arg5_) {}
435 #undef NLIB_THFUNC_USESWAP_NO 436 #undef NLIB_THFUNC_USESWAP_YES 438 template <
class FUNC>
439 struct ThreadFuncX0 :
public Args0<FUNC> {
440 typedef ThreadFuncX0<FUNC> ThisType;
441 typedef Args0<FUNC> BaseType;
443 ThreadFuncX0(
const FUNC& func_, B b)
444 : BaseType(func_, b) {}
445 explicit ThreadFuncX0(
const FUNC& func_) : BaseType(func_, None()) {}
450 template <
class FUNC,
class T1>
451 struct ThreadFuncX1 :
public Args1<FUNC, T1> {
452 typedef ThreadFuncX1<FUNC, T1> ThisType;
453 typedef Args1<FUNC, T1> BaseType;
455 ThreadFuncX1(
const FUNC& func_,
const T1& arg1_, B b)
456 : BaseType(func_, arg1_, b) {}
457 ThreadFuncX1(
const FUNC& func_,
const T1& arg1_) : BaseType(func_, arg1_, None()) {}
459 ptr->func(NLIB_MOVE(ptr->arg1));
462 template <
class FUNC,
class T1,
class T2>
463 struct ThreadFuncX2 :
public Args2<FUNC, T1, T2> {
464 typedef ThreadFuncX2<FUNC, T1, T2> ThisType;
465 typedef Args2<FUNC, T1, T2> BaseType;
467 ThreadFuncX2(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_, B b)
468 : BaseType(func_, arg1_, arg2_, b) {}
469 ThreadFuncX2(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_)
470 : BaseType(func_, arg1_, arg2_, None()) {}
472 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2));
475 template <
class FUNC,
class T1,
class T2,
class T3>
476 struct ThreadFuncX3 :
public Args3<FUNC, T1, T2, T3> {
477 typedef ThreadFuncX3<FUNC, T1, T2, T3> ThisType;
478 typedef Args3<FUNC, T1, T2, T3> BaseType;
480 ThreadFuncX3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_, B b)
481 : BaseType(func_, arg1_, arg2_, arg3_, b) {}
482 ThreadFuncX3(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_)
483 : BaseType(func_, arg1_, arg2_, arg3_, None()) {}
485 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3));
488 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
489 struct ThreadFuncX4 :
public Args4<FUNC, T1, T2, T3, T4> {
490 typedef ThreadFuncX4<FUNC, T1, T2, T3, T4> ThisType;
491 typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
493 ThreadFuncX4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
494 const T4& arg4_, B b)
495 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b) {}
496 ThreadFuncX4(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
498 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, None()) {}
500 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
501 NLIB_MOVE(ptr->arg4));
504 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
505 struct ThreadFuncX5 :
public Args5<FUNC, T1, T2, T3, T4, T5> {
506 typedef ThreadFuncX5<FUNC, T1, T2, T3, T4, T5> ThisType;
507 typedef Args5<FUNC, T1, T2, T3, T4, T5> BaseType;
509 ThreadFuncX5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
510 const T4& arg4_,
const T5& arg5_, B b)
511 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, b) {}
512 ThreadFuncX5(
const FUNC& func_,
const T1& arg1_,
const T2& arg2_,
const T3& arg3_,
513 const T4& arg4_,
const T5& arg5_)
514 : BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, None()) {}
516 ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
517 NLIB_MOVE(ptr->arg4), NLIB_MOVE(ptr->arg5));
522 template <
class FUNC>
525 new (std::nothrow) ThreadFuncX0<FUNC>(f,
typename IsSwappable<FUNC>::type()));
526 if (!ptr)
return ENOMEM;
527 return this->StartRaw(settings, ptr);
529 template <
class FUNC>
532 new (std::nothrow) ThreadFuncX0<FUNC>(f,
typename IsSwappable<FUNC>::type()));
533 if (!ptr)
return ENOMEM;
534 return this->StartRaw(ptr);
536 template <
class FUNC,
class T1>
539 new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1,
typename ArgType<T1>::type()));
540 if (!ptr)
return ENOMEM;
541 return this->StartRaw(settings, ptr);
543 template <
class FUNC,
class T1>
546 new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1,
typename ArgType<T1>::type()));
547 if (!ptr)
return ENOMEM;
548 return this->StartRaw(ptr);
550 template <
class FUNC,
class T1,
class T2>
554 f, arg1, arg2,
typename ArgType<T2>::type()));
555 if (!ptr)
return ENOMEM;
556 return this->StartRaw(settings, ptr);
558 template <
class FUNC,
class T1,
class T2>
561 f, arg1, arg2,
typename ArgType<T2>::type()));
562 if (!ptr)
return ENOMEM;
563 return this->StartRaw(ptr);
565 template <
class FUNC,
class T1,
class T2,
class T3>
570 ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3,
typename ArgType<T3>::type()));
571 if (!ptr)
return ENOMEM;
572 return this->StartRaw(settings, ptr);
574 template <
class FUNC,
class T1,
class T2,
class T3>
578 ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3,
typename ArgType<T3>::type()));
579 if (!ptr)
return ENOMEM;
580 return this->StartRaw(ptr);
582 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
584 const T3& arg3,
const T4& arg4,
move_tag) {
586 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
587 typename ArgType<T4>::type()));
588 if (!ptr)
return ENOMEM;
589 return this->StartRaw(settings, ptr);
591 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
592 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
595 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
596 typename ArgType<T4>::type()));
597 if (!ptr)
return ENOMEM;
598 return this->StartRaw(ptr);
600 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
602 const T3& arg3,
const T4& arg4,
const T5& arg5,
move_tag) {
604 new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
605 f, arg1, arg2, arg3, arg4, arg5,
typename ArgType<T5>::type()));
606 if (!ptr)
return ENOMEM;
607 return this->StartRaw(settings, ptr);
609 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
610 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
613 new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
614 f, arg1, arg2, arg3, arg4, arg5,
typename ArgType<T5>::type()));
615 if (!ptr)
return ENOMEM;
616 return this->StartRaw(ptr);
618 template <
class FUNC>
621 if (!ptr)
return ENOMEM;
622 return this->StartRaw(settings, ptr);
624 template <
class FUNC>
627 if (!ptr)
return ENOMEM;
628 return this->StartRaw(ptr);
630 template <
class FUNC,
class T1>
633 if (!ptr)
return ENOMEM;
634 return this->StartRaw(settings, ptr);
636 template <
class FUNC,
class T1>
639 if (!ptr)
return ENOMEM;
640 return this->StartRaw(ptr);
642 template <
class FUNC,
class T1,
class T2>
645 ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
646 if (!ptr)
return ENOMEM;
647 return this->StartRaw(settings, ptr);
649 template <
class FUNC,
class T1,
class T2>
652 ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
653 if (!ptr)
return ENOMEM;
654 return this->StartRaw(ptr);
656 template <
class FUNC,
class T1,
class T2,
class T3>
660 new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
661 if (!ptr)
return ENOMEM;
662 return this->StartRaw(settings, ptr);
664 template <
class FUNC,
class T1,
class T2,
class T3>
665 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3) {
667 new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
668 if (!ptr)
return ENOMEM;
669 return this->StartRaw(ptr);
671 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
673 const T3& arg3,
const T4& arg4) {
675 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
676 if (!ptr)
return ENOMEM;
677 return this->StartRaw(settings, ptr);
679 template <
class FUNC,
class T1,
class T2,
class T3,
class T4>
680 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4) {
682 new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
683 if (!ptr)
return ENOMEM;
684 return this->StartRaw(ptr);
686 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
688 const T3& arg3,
const T4& arg4,
const T5& arg5) {
690 std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
691 if (!ptr)
return ENOMEM;
692 return this->StartRaw(settings, ptr);
694 template <
class FUNC,
class T1,
class T2,
class T3,
class T4,
class T5>
695 errno_t Start(
const FUNC& f,
const T1& arg1,
const T2& arg2,
const T3& arg3,
const T4& arg4,
698 std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
699 if (!ptr)
return ENOMEM;
700 return this->StartRaw(ptr);
713 struct Thread::Args0<void()> {
715 Args0(
void (*func_)(), X)
717 Args0(
void (*func_)()) : func(func_) {}
725 struct Thread::Args0<void(T1)> {
727 Args0(
void (*func_)(T1), X)
729 Args0(
void (*func_)(T1)) : func(func_) {}
736 template <
class T1,
class T2>
737 struct Thread::Args0<void(T1, T2)> {
739 Args0(
void (*func_)(T1, T2), X)
741 Args0(
void (*func_)(T1, T2)) : func(func_) {}
742 void (*func)(T1, T2);
748 template <
class T1,
class T2,
class T3>
749 struct Thread::Args0<void(T1, T2, T3)> {
751 Args0(
void (*func_)(T1, T2, T3), X)
753 Args0(
void (*func_)(T1, T2, T3)) : func(func_) {}
754 void (*func)(T1, T2, T3);
760 template <
class T1,
class T2,
class T3,
class T4>
761 struct Thread::Args0<void(T1, T2, T3, T4)> {
763 Args0(
void (*func_)(T1, T2, T3, T4), X)
765 Args0(
void (*func_)(T1, T2, T3, T4)) : func(func_) {}
766 void (*func)(T1, T2, T3, T4);
772 template <
class T1,
class T2,
class T3,
class T4,
class T5>
773 struct Thread::Args0<void(T1, T2, T3, T4, T5)> {
775 Args0(
void (*func_)(T1, T2, T3, T4, T5), X)
777 Args0(
void (*func_)(T1, T2, T3, T4, T5)) : func(func_) {}
778 void (*func)(T1, T2, T3, T4, T5);
790 namespace this_thread {
801 return e == 0 ?
id : -1;
807 if (e != 0)
return e;
818 NLIB_DEFINE_STD_SWAP(::nlib_ns::threading::Thread)
820 #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...