nlib
Thread.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_THREADING_THREAD_H_
17 #define INCLUDE_NN_NLIB_THREADING_THREAD_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 #include "nn/nlib/UniquePtr.h"
22 #include "nn/nlib/TypeTraits.h"
23 #include "nn/nlib/DateTime.h"
24 
25 NLIB_NAMESPACE_BEGIN
26 namespace threading {
27 struct None {};
28 
30  public:
31  ThreadSettings() NLIB_NOEXCEPT : is_initialized_(false) {}
33  if (is_initialized_) {
35  NLIB_ASSERT_NOERR(e);
36  NLIB_UNUSED(e);
37  }
38  }
39  void SetDetachState(bool detached) NLIB_NOEXCEPT {
40  this->Init_();
42  &attr_,
43  NLIB_THREAD_ATTR_KEY_DETACHSTATE,
44  detached ? 1 : 0);
45  NLIB_ASSERT_NOERR(e);
46  NLIB_UNUSED(e);
47  }
49  this->Init_();
50  int detached;
52  &attr_,
53  NLIB_THREAD_ATTR_KEY_DETACHSTATE,
54  &detached);
55  NLIB_ASSERT_NOERR(e);
56  NLIB_UNUSED(e);
57  return (detached != 0);
58  }
60  this->Init_();
62  &attr_,
63  NLIB_THREAD_ATTR_KEY_STACKSIZE,
64  size);
65  return e;
66  }
68  this->Init_();
69  int size;
71  &attr_,
72  NLIB_THREAD_ATTR_KEY_STACKSIZE,
73  &size);
74  NLIB_ASSERT_NOERR(e);
75  NLIB_UNUSED(e);
76  return size;
77  }
79  this->Init_();
81  &attr_,
82  NLIB_THREAD_ATTR_KEY_PRIORITY,
83  priority);
84  return e;
85  }
86  int GetPriority() const NLIB_NOEXCEPT {
87  this->Init_();
88  int priority;
90  &attr_,
91  NLIB_THREAD_ATTR_KEY_PRIORITY,
92  &priority);
93  NLIB_ASSERT_NOERR(e);
94  NLIB_UNUSED(e);
95  return priority;
96  }
98  return is_initialized_ ? &attr_ : nullptr;
99  }
100  const nlib_thread_attr* GetPtr() const NLIB_NOEXCEPT {
101  return is_initialized_ ? &attr_ : nullptr;
102  }
103 
104  private:
105  void Init_() const NLIB_NOEXCEPT {
106  if (!is_initialized_) {
107  errno_t e = nlib_thread_attr_init(&attr_);
108  NLIB_ASSERT_NOERR(e);
109  NLIB_UNUSED(e);
110  is_initialized_ = true;
111  }
112  }
113  mutable bool is_initialized_;
114  mutable nlib_thread_attr attr_;
115 };
116 
117 template <class T1 = None, class T2 = None, class T3 = None, class T4 = None, class T5 = None>
118 struct ThreadArg {
121  typedef void (*Func)(ArgType& ptr); // NOLINT
122  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2(), arg3(), arg4(), arg5() {}
123  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_)
124  : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_), arg4(arg4_), arg5(arg5_) {}
125  static void Call(ArgType& ptr) { ptr->func(ptr); } // NOLINT
126  public:
127  Func func;
128  T1 arg1;
129  T2 arg2;
130  T3 arg3;
131  T4 arg4;
132  T5 arg5;
133 };
134 
135 template <>
136 struct ThreadArg<None, None, None, None, None> {
137  typedef ThreadArg<> ThisType;
138  typedef void ArgType;
139  typedef void (*Func)();
140 };
141 
142 template <class T1>
143 struct ThreadArg<T1, None, None, None, None> {
144  typedef ThreadArg<T1> ThisType;
146  typedef void (*Func)(ArgType& ptr); // NOLINT
147  NLIB_CEXPR ThreadArg() : func(nullptr), arg1() {}
148  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_) : func(func_), arg1(arg1_) {}
149  static void Call(ArgType& ptr) { ptr->func(ptr); } // NOLINT
150  public:
151  Func func;
152  T1 arg1;
153 };
154 
155 template <class T1, class T2>
156 struct ThreadArg<T1, T2, None, None, None> {
157  typedef ThreadArg<T1, T2> ThisType;
159  typedef void (*Func)(ArgType& ptr); // NOLINT
160  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2() {}
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); } // NOLINT
163  public:
164  Func func;
165  T1 arg1;
166  T2 arg2;
167 };
168 
169 template <class T1, class T2, class T3>
170 struct ThreadArg<T1, T2, T3, None, None> {
173  typedef void (*Func)(ArgType& ptr); // NOLINT
174  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2(), arg3() {}
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); } // NOLINT
178  public:
179  Func func;
180  T1 arg1;
181  T2 arg2;
182  T3 arg3;
183 };
184 
185 template <class T1, class T2, class T3, class T4>
186 struct ThreadArg<T1, T2, T3, T4, None> {
189  typedef void (*Func)(ArgType& ptr); // NOLINT
190  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2(), arg3(), arg4() {}
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); } // NOLINT
194  public:
195  Func func;
196  T1 arg1;
197  T2 arg2;
198  T3 arg3;
199  T4 arg4;
200 };
201 
202 #ifdef _MSC_VER
203 #pragma warning(push)
204 #pragma warning(disable : 4180)
205 #endif
206 
207 // code snippets:
208 // Thread th;
209 // void func(...) {}
210 // th.Start(func, ...); # to create a thread and run func() on it
211 // th.Join(); # or th.Detach();
213  public:
214  typedef void (*ThreadFunc)(void* arg);
217  if (thread_id_ != NLIB_THREAD_INVALID) this->Join();
218  }
219 #ifdef __cpp_rvalue_references
220  Thread(Thread&& rhs) NLIB_NOEXCEPT : thread_id_(rhs.thread_id_) {
221  rhs.thread_id_ = NLIB_THREAD_INVALID;
222  }
224  thread_id_ = rhs.thread_id_;
225  rhs.thread_id_ = NLIB_THREAD_INVALID;
226  return *this;
227  }
228 #endif
229  Thread(Thread& rhs, move_tag) NLIB_NOEXCEPT : thread_id_(rhs.thread_id_) { // NOLINT
230  rhs.thread_id_ = NLIB_THREAD_INVALID;
231  }
233  thread_id_ = rhs.thread_id_;
234  rhs.thread_id_ = NLIB_THREAD_INVALID;
235  return *this;
236  }
237  NLIB_DEPRECATED void swap(Thread& rhs) NLIB_NOEXCEPT { // NOLINT
238  using std::swap;
239  swap(thread_id_, rhs.thread_id_);
240  }
241  errno_t StartRaw(ThreadFunc func, void* arg) NLIB_NOEXCEPT;
242  errno_t StartRaw(ThreadFunc func, void* arg, const ThreadSettings& settings) NLIB_NOEXCEPT;
243  errno_t Join() NLIB_NOEXCEPT {
244  if (!thread_id_) return ESRCH;
245  errno_t e = nlib_thread_join(thread_id_);
246  if (e == 0) {
247  thread_id_ = NLIB_THREAD_INVALID;
248  return 0;
249  } else {
250  return e;
251  }
252  }
253  errno_t Detach() NLIB_NOEXCEPT {
254  errno_t e = nlib_thread_detach(thread_id_);
255  if (e == 0) thread_id_ = NLIB_THREAD_INVALID;
256  return e;
257  }
258  bool IsJoinable() const NLIB_NOEXCEPT { return thread_id_ != NLIB_THREAD_INVALID; }
259  errno_t GetPriority(int32_t* priority) NLIB_NOEXCEPT {
260  return nlib_thread_getpriority(this->GetNativeHandle(), reinterpret_cast<int*>(priority));
261  }
262  errno_t ChangePriority(int32_t priority) NLIB_NOEXCEPT {
263  return nlib_thread_setpriority(this->GetNativeHandle(), priority);
264  }
265  errno_t SetAffinity(uint32_t affinity) NLIB_NOEXCEPT {
266  return nlib_thread_setaffinity(thread_id_, affinity);
267  }
268  nlib_thread GetNativeHandle() const NLIB_NOEXCEPT { return thread_id_; }
269  bool operator==(const Thread& rhs) NLIB_NOEXCEPT {
270  return nlib_thread_equal(thread_id_, rhs.thread_id_) ? true : false;
271  }
272  bool operator!=(const Thread& rhs) NLIB_NOEXCEPT {
273  return nlib_thread_equal(thread_id_, rhs.thread_id_) ? false : true;
274  }
275 
276  static errno_t YieldThread() NLIB_NOEXCEPT { return nlib_yield(); }
277  static errno_t Sleep(const TimeSpan& span) NLIB_NOEXCEPT {
278  return nlib_sleep(span.ToTimeValue().tick);
279  }
280 
281  public:
282  errno_t StartRaw(const ThreadSettings& settings, ThreadArg<>::Func func) NLIB_NOEXCEPT {
283  errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func), settings);
284  return e;
285  }
286  errno_t StartRaw(ThreadArg<>::Func func) NLIB_NOEXCEPT {
287  errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func));
288  return e;
289  }
290  template <class ThArg>
291  errno_t StartRaw(const ThreadSettings& settings,
292  UniquePtr<ThArg>& ptr) NLIB_NOEXCEPT { // NOLINT
293  if (!ptr) return EINVAL;
294  errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>, // NOLINT
295  reinterpret_cast<void*>(ptr.get()), settings);
296  if (e == 0) ptr.release();
297  return e;
298  }
299  template <class ThArg>
300  errno_t StartRaw(UniquePtr<ThArg>& ptr) NLIB_NOEXCEPT { // NOLINT
301  if (!ptr) return EINVAL;
302  errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>, // NOLINT
303  reinterpret_cast<void*>(ptr.get()));
304  if (e == 0) ptr.release();
305  return e;
306  }
307 
308  private:
309  static void Exec(void* p) {
310  ThreadArg<>::Func f = reinterpret_cast<ThreadArg<>::Func>(p);
311  f();
312  }
313  template <class T>
314  static void Exec(void* p) {
315  UniquePtr<T> args(reinterpret_cast<T*>(p));
316  T::Call(args);
317  }
318 
319 #define NLIB_THFUNC_USESWAP_NO(tp) \
320  typename EnableIf< \
321  !IsSame<None, typename RemoveCv<B>::type>::value && \
322  (IsSame<FalseType, typename RemoveCv<B>::type>::value || !IsSwappable<tp>::value), \
323  const tp&>::type
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, \
328  const tp&>::type
329 
330  template <class T>
331  struct ArgType : public Conditional<
332  IsArithmetic<T>::value || IsPointer<T>::value || IsMemberPointer<T>::value,
333  FalseType, TrueType> {};
334 
335  template <class FUNC>
336  struct Args0 {
337  typedef typename RemoveRef<FUNC>::type FUNC_;
338  template <class B>
339  Args0(NLIB_THFUNC_USESWAP_NO(FUNC_) func_, B)
340  : func(func_) {}
341  template <class B>
342  Args0(NLIB_THFUNC_USESWAP_YES(FUNC_) func_, B)
343  : func() {
344  using std::swap;
345  swap(const_cast<FUNC&>(func_), func);
346  }
347  template <class B>
348  Args0(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
349  func_,
350  B)
351  : func(func_) {}
352  FUNC_ func;
353 
354  private:
356  };
357  template <class FUNC, class T1>
358  struct Args1 : public Args0<FUNC> {
359  typedef Args0<FUNC> BaseType;
360  typedef typename RemoveRef<T1>::type T1_;
361  template <class B>
362  Args1(const FUNC& func_, NLIB_THFUNC_USESWAP_NO(T1) arg1_, B)
363  : BaseType(func_, typename IsSwappable<FUNC>::type()), arg1(arg1_) {}
364  template <class B>
365  Args1(const FUNC& func_, NLIB_THFUNC_USESWAP_YES(T1) arg1_, B)
366  : BaseType(func_, typename IsSwappable<FUNC>::type()), arg1() {
367  using std::swap;
368  swap(const_cast<T1&>(arg1_), arg1);
369  }
370  template <class B>
371  Args1(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
372  func_,
373  const T1& arg1_, B b)
374  : BaseType(func_, b), arg1(arg1_) {}
375  T1_ arg1;
376  };
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_;
381  template <class B>
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_) {}
384  template <class B>
385  Args2(const FUNC& func_, const T1& arg1_, NLIB_THFUNC_USESWAP_YES(T2) arg2_, B)
386  : BaseType(func_, arg1_, typename ArgType<T1>::type()), arg2() {
387  using std::swap;
388  swap(const_cast<T2&>(arg2_), arg2);
389  }
390  template <class B>
391  Args2(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
392  func_,
393  const T1& arg1_, const T2& arg2_, B b)
394  : BaseType(func_, arg1_, b), arg2(arg2_) {}
395  T2_ arg2;
396  };
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_;
401  template <class B>
402  Args3(const FUNC& func_, const T1& arg1_, const T2& arg2_, NLIB_THFUNC_USESWAP_NO(T3) arg3_,
403  B)
404  : BaseType(func_, arg1_, arg2_, typename ArgType<T2>::type()), arg3(arg3_) {}
405  template <class B>
406  Args3(const FUNC& func, const T1& arg1_, const T2& arg2_, NLIB_THFUNC_USESWAP_YES(T3) arg3_,
407  B)
408  : BaseType(func, arg1_, arg2_, typename ArgType<T2>::type()), arg3() {
409  using std::swap;
410  swap(const_cast<T3&>(arg3_), arg3);
411  }
412  template <class B>
413  Args3(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
414  func_,
415  const T1& arg1_, const T2& arg2_, const T3& arg3_, B b)
416  : BaseType(func_, arg1_, arg2_, b), arg3(arg3_) {}
417  T3_ arg3;
418  };
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_;
423  template <class B>
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_) {}
427  template <class B>
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() {
431  using std::swap;
432  swap(const_cast<T4&>(arg4_), arg4);
433  }
434  template <class B>
435  Args4(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
436  func_,
437  const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_, B b)
438  : BaseType(func_, arg1_, arg2_, arg3_, b), arg4(arg4_) {}
439  T4_ arg4;
440  };
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_;
445  template <class B>
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()),
449  arg5(arg5_) {}
450  template <class B>
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() {
454  using std::swap;
455  swap(const_cast<T5&>(arg5_), arg5);
456  }
457  template <class B>
458  Args5(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
459  func_,
460  const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_, const T5& arg5_,
461  B b)
462  : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b), arg5(arg5_) {}
463  T5_ arg5;
464  };
465 #undef NLIB_THFUNC_USESWAP_NO
466 #undef NLIB_THFUNC_USESWAP_YES
467 
468  template <class FUNC>
469  struct ThreadFuncX0 : public Args0<FUNC> {
470  typedef ThreadFuncX0<FUNC> ThisType;
471  typedef Args0<FUNC> BaseType;
472  template <class B>
473  ThreadFuncX0(const FUNC& func_, B b)
474  : BaseType(func_, b) {}
475  explicit ThreadFuncX0(const FUNC& func_) : BaseType(func_, None()) {}
476  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
477  ptr->func();
478  }
479  };
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;
484  template <class B>
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()) {}
488  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
489  ptr->func(NLIB_MOVE(ptr->arg1));
490  }
491  };
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;
496  template <class B>
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()) {}
501  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
502  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2));
503  }
504  };
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;
509  template <class B>
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()) {}
514  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
515  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3));
516  }
517  };
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;
522  template <class B>
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_,
527  const T4& arg4_)
528  : BaseType(func_, arg1_, arg2_, arg3_, arg4_, None()) {}
529  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
530  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
531  NLIB_MOVE(ptr->arg4));
532  }
533  };
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;
538  template <class B>
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()) {}
545  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
546  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
547  NLIB_MOVE(ptr->arg4), NLIB_MOVE(ptr->arg5));
548  }
549  };
550 
551  public:
552  template <class FUNC>
553  errno_t Start(const ThreadSettings& settings, const FUNC& f, move_tag) {
555  new (std::nothrow) ThreadFuncX0<FUNC>(f, typename IsSwappable<FUNC>::type()));
556  if (!ptr) return ENOMEM;
557  return this->StartRaw(settings, ptr);
558  }
559  template <class FUNC>
560  errno_t Start(const FUNC& f, move_tag) {
562  new (std::nothrow) ThreadFuncX0<FUNC>(f, typename IsSwappable<FUNC>::type()));
563  if (!ptr) return ENOMEM;
564  return this->StartRaw(ptr);
565  }
566  template <class FUNC, class T1>
567  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, move_tag) {
569  new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1, typename ArgType<T1>::type()));
570  if (!ptr) return ENOMEM;
571  return this->StartRaw(settings, ptr);
572  }
573  template <class FUNC, class T1>
574  errno_t Start(const FUNC& f, const T1& arg1, move_tag) {
576  new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1, typename ArgType<T1>::type()));
577  if (!ptr) return ENOMEM;
578  return this->StartRaw(ptr);
579  }
580  template <class FUNC, class T1, class T2>
581  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
582  move_tag) {
583  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow) ThreadFuncX2<FUNC, T1, T2>(
584  f, arg1, arg2, typename ArgType<T2>::type()));
585  if (!ptr) return ENOMEM;
586  return this->StartRaw(settings, ptr);
587  }
588  template <class FUNC, class T1, class T2>
589  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, move_tag) {
590  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow) ThreadFuncX2<FUNC, T1, T2>(
591  f, arg1, arg2, typename ArgType<T2>::type()));
592  if (!ptr) return ENOMEM;
593  return this->StartRaw(ptr);
594  }
595  template <class FUNC, class T1, class T2, class T3>
596  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
597  const T3& arg3, move_tag) {
599  new (std::nothrow)
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);
603  }
604  template <class FUNC, class T1, class T2, class T3>
605  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, move_tag) {
607  new (std::nothrow)
608  ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3, typename ArgType<T3>::type()));
609  if (!ptr) return ENOMEM;
610  return this->StartRaw(ptr);
611  }
612  template <class FUNC, class T1, class T2, class T3, class T4>
613  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
620  }
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,
623  move_tag) {
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);
629  }
630  template <class FUNC, class T1, class T2, class T3, class T4, class T5>
631  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
638  }
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,
641  const T5& arg5, move_tag) {
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);
647  }
648  template <class FUNC>
649  errno_t Start(const ThreadSettings& settings, const FUNC& f) {
650  UniquePtr<ThreadFuncX0<FUNC> > ptr(new (std::nothrow) ThreadFuncX0<FUNC>(f));
651  if (!ptr) return ENOMEM;
652  return this->StartRaw(settings, ptr);
653  }
654  template <class FUNC>
655  errno_t Start(const FUNC& f) {
656  UniquePtr<ThreadFuncX0<FUNC> > ptr(new (std::nothrow) ThreadFuncX0<FUNC>(f));
657  if (!ptr) return ENOMEM;
658  return this->StartRaw(ptr);
659  }
660  template <class FUNC, class T1>
661  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1) {
662  UniquePtr<ThreadFuncX1<FUNC, T1> > ptr(new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1));
663  if (!ptr) return ENOMEM;
664  return this->StartRaw(settings, ptr);
665  }
666  template <class FUNC, class T1>
667  errno_t Start(const FUNC& f, const T1& arg1) {
668  UniquePtr<ThreadFuncX1<FUNC, T1> > ptr(new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1));
669  if (!ptr) return ENOMEM;
670  return this->StartRaw(ptr);
671  }
672  template <class FUNC, class T1, class T2>
673  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2) {
674  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow)
675  ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
676  if (!ptr) return ENOMEM;
677  return this->StartRaw(settings, ptr);
678  }
679  template <class FUNC, class T1, class T2>
680  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2) {
681  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow)
682  ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
683  if (!ptr) return ENOMEM;
684  return this->StartRaw(ptr);
685  }
686  template <class FUNC, class T1, class T2, class T3>
687  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
688  const T3& arg3) {
690  new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
691  if (!ptr) return ENOMEM;
692  return this->StartRaw(settings, ptr);
693  }
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);
700  }
701  template <class FUNC, class T1, class T2, class T3, class T4>
702  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
708  }
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);
715  }
716  template <class FUNC, class T1, class T2, class T3, class T4, class T5>
717  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
723  }
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,
726  const T5& arg5) {
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);
731  }
732 
733  private:
734  nlib_thread thread_id_;
736 };
737 
738 #ifdef _MSC_VER
739 #pragma warning(pop)
740 #endif
741 
742 template <>
743 struct Thread::Args0<void()> {
744  template <class X>
745  Args0(void (*func_)(), X)
746  : func(func_) {}
747  Args0(void (*func_)()) : func(func_) {}
748  void (*func)();
749 
750  private:
752 };
753 
754 template <class T1>
755 struct Thread::Args0<void(T1)> {
756  template <class X>
757  Args0(void (*func_)(T1), X)
758  : func(func_) {}
759  Args0(void (*func_)(T1)) : func(func_) {}
760  void (*func)(T1);
761 
762  private:
764 };
765 
766 template <class T1, class T2>
767 struct Thread::Args0<void(T1, T2)> {
768  template <class X>
769  Args0(void (*func_)(T1, T2), X)
770  : func(func_) {}
771  Args0(void (*func_)(T1, T2)) : func(func_) {}
772  void (*func)(T1, T2);
773 
774  private:
776 };
777 
778 template <class T1, class T2, class T3>
779 struct Thread::Args0<void(T1, T2, T3)> {
780  template <class X>
781  Args0(void (*func_)(T1, T2, T3), X)
782  : func(func_) {}
783  Args0(void (*func_)(T1, T2, T3)) : func(func_) {}
784  void (*func)(T1, T2, T3);
785 
786  private:
788 };
789 
790 template <class T1, class T2, class T3, class T4>
791 struct Thread::Args0<void(T1, T2, T3, T4)> {
792  template <class X>
793  Args0(void (*func_)(T1, T2, T3, T4), X)
794  : func(func_) {}
795  Args0(void (*func_)(T1, T2, T3, T4)) : func(func_) {}
796  void (*func)(T1, T2, T3, T4);
797 
798  private:
800 };
801 
802 template <class T1, class T2, class T3, class T4, class T5>
803 struct Thread::Args0<void(T1, T2, T3, T4, T5)> {
804  template <class X>
805  Args0(void (*func_)(T1, T2, T3, T4, T5), X)
806  : func(func_) {}
807  Args0(void (*func_)(T1, T2, T3, T4, T5)) : func(func_) {}
808  void (*func)(T1, T2, T3, T4, T5);
809 
810  private:
812 };
813 
815  unsigned int num_cpu;
816  (void)nlib_thread_getconcurrency(&num_cpu);
817  return num_cpu;
818 }
819 
820 namespace this_thread {
822 inline errno_t Sleep(const TimeSpan& span) NLIB_NOEXCEPT {
824 }
825 inline errno_t SleepMilliSeconds(unsigned int millisec) NLIB_NOEXCEPT {
826  return Sleep(TimeSpan(0, 0, millisec));
827 }
829  nlib_thread_id id;
830  errno_t e = nlib_thread_getid(&id);
831  return e == 0 ? id : -1;
832 }
833 inline errno_t GetCpu(int* cpuid) NLIB_NOEXCEPT { return nlib_thread_getcpu(cpuid); }
834 inline errno_t SetName(const char* literal_string) NLIB_NOEXCEPT {
835  nlib_thread th;
836  errno_t e = nlib_thread_self(&th);
837  if (e != 0) return e;
838  return nlib_thread_setname(th, literal_string);
839 }
842 
843 } // namespace this_thread
844 
845 } // namespace threading
846 NLIB_NAMESPACE_END
847 
848 NLIB_DEFINE_STD_SWAP(::nlib_ns::threading::Thread)
849 
850 #endif // INCLUDE_NN_NLIB_THREADING_THREAD_H_
errno_t GetCpu(int *cpuid) noexcept
呼び出したスレッドが実行されているCPUを取得します。
Definition: Thread.h:833
errno_t YieldThread() noexcept
他のスレッドに制御を譲ります。
Definition: Thread.h:821
errno_t Start(const ThreadSettings &settings, const FUNC &f, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:553
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
引数を2つもつ関数を別スレッドで実行開始します。 引数を2つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:589
nlib_thread GetNativeHandle() const noexcept
実装依存のスレッド識別子を取得します。
Definition: Thread.h:268
static errno_t Sleep(const TimeSpan &span) noexcept
span の期間だけスリープします。
Definition: Thread.h:277
int nlib_thread_equal(nlib_thread th1, nlib_thread th2)
2つのスレッドが同一スレッドを指すかどうかチェックします。
constexpr Thread() noexcept
デフォルトコンストラクタです。
Definition: Thread.h:215
int GetStackSize() const noexcept
設定されているスタックサイズを返します。
Definition: Thread.h:67
errno_t Sleep(const TimeSpan &span) noexcept
スリープします。
Definition: Thread.h:822
constexpr ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_)
コンストラクタで構造体のフィールドを初期化します。
Definition: Thread.h:123
C++11の標準ヘッダとなるtype_traitsの代用定義です。 コンパイラや標準ライブラリによってサポートされてい...
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:687
bool operator!=(const Thread &rhs) noexcept
nlib_thread_equal()を用いてスレッドを比較します。
Definition: Thread.h:272
constexpr ThreadArg()
デフォルトコンストラクタです。
Definition: Thread.h:122
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
T1 arg1
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:128
errno_t Detach() noexcept
スレッドをデタッチします。
Definition: Thread.h:253
Func func
別スレッドで実行する関数へのポインタです。
Definition: Thread.h:127
bool GetDetachState() const noexcept
デタッチ状態でスレッドを起動する設定かどうかを返します。
Definition: Thread.h:48
errno_t nlib_thread_setaffinity(nlib_thread thread, uint32_t affinity)
指定されたスレッドのプロセッサアフィニティマスクを設定します。
errno_t GetPriority(int32_t *priority) noexcept
スレッドの優先度を取得します。
Definition: Thread.h:259
T3 arg3
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:130
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, move_tag)
引数を5つもつ関数を別スレッドで実行開始します。引数を5つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:640
~Thread() noexcept
デストラクタです。スレッドがJoinされていない場合はJoinします。
Definition: Thread.h:216
struct nlib_thread_attr_ nlib_thread_attr
新しく作られるスレッドに適用されるスレッド属性
Definition: Platform.h:1237
errno_t ChangePriority(int32_t priority) noexcept
スレッドの優先度を設定します。
Definition: Thread.h:262
T5 arg5
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:132
void SetDetachState(bool detached) noexcept
デタッチした状態でスレッドを起動するかどうかを設定します。
Definition: Thread.h:39
errno_t nlib_thread_self(nlib_thread *thread)
実行中のスレッドに対応するnlib_threadの値を格納する。
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2)
引数を2つもつ関数を別スレッドで実行開始します。引数を2つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:680
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
引数を3つもつ関数を別スレッドで実行開始します。引数を3つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:695
UniquePtrはポインタの所有権を保持し、UniquePtrがスコープから出るときにデストラクタでポインタをDELで指...
Definition: UniquePtr.h:109
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
引数を5つもつ関数を別スレッドで実行開始します。引数を5つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:725
std::unique_ptrに相当するクラスが定義されています。
スレッドの生成・開始を行うためのクラスです。
Definition: Thread.h:212
errno_t nlib_thread_getid(nlib_thread_id *id)
実行中のスレッドに対応する一意の整数値を格納する。
Threadクラスでスレッドを実行するために利用できる構造体です。
Definition: Thread.h:118
void swap(Thread &rhs) noexcept
オブジェクトの内容をスワップします。
Definition: Thread.h:237
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
引数を3つもつ関数を別スレッドで実行開始します。 引数を3つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:605
errno_t nlib_thread_attr_setint(nlib_thread_attr *attr, int key, int value)
スレッドの属性オブジェクトのキーに対応する整数を設定する。
#define NLIB_DEPRECATED
関数等がdeprecatedになったことを示します。
Definition: Config.h:109
int GetPriority() const noexcept
スレッドの優先度を取得します。
Definition: Thread.h:86
bool IsJoinable() const noexcept
join可能かどうか調べます。
Definition: Thread.h:258
Thread(Thread &rhs, move_tag) noexcept
ムーブコンストラクタに相当します。
Definition: Thread.h:229
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
errno_t nlib_thread_getconcurrency(unsigned int *num_cpu)
ハードウェアスレッドの数を取得します。
errno_t SetStackSize(int size) noexcept
スタックサイズを設定します。
Definition: Thread.h:59
errno_t Start(const FUNC &f, const T1 &arg1, move_tag)
引数を1つもつ関数を別スレッドで実行開始します。
Definition: Thread.h:574
errno_t StartRaw(UniquePtr< ThArg > &ptr) noexcept
別スレッドの実行を開始します。
Definition: Thread.h:300
errno_t SetName(const char *literal_string) noexcept
スレッドに名前をつけます。
Definition: Thread.h:834
nlib_thread_attrをラップするクラスです。必要に応じて自動的にnlib_thread_attr_init()とnlib_thread_attr...
Definition: Thread.h:29
errno_t StartRaw(const ThreadSettings &settings, UniquePtr< ThArg > &ptr) noexcept
別スレッドの実行を開始します。
Definition: Thread.h:291
Thread & assign(Thread &rhs, move_tag) noexcept
ムーブ代入演算子に相当します。
Definition: Thread.h:232
static errno_t YieldThread() noexcept
nlib_yield()を呼び出します。
Definition: Thread.h:276
errno_t Join() noexcept
スレッドの実行完了を待ちます。
Definition: Thread.h:243
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:596
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:265
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:661
bool operator==(const Thread &rhs) noexcept
nlib_thread_equal()を用いてスレッドを比較します。
Definition: Thread.h:269
errno_t nlib_thread_attr_destroy(nlib_thread_attr *attr)
スレッド初期化オブジェクトを破壊します。
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
引数を4つもつ関数を別スレッドで実行開始します。引数を4つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:710
errno_t Start(const FUNC &f, const T1 &arg1)
引数を1つもつ関数を別スレッドで実行開始します。
Definition: Thread.h:667
#define NLIB_THREAD_INVALID
無効なスレッドを指し示す値
Definition: Platform.h:1221
errno_t nlib_thread_attr_getint(const nlib_thread_attr *attr, int key, int *value)
スレッドの属性オブジェクトのキーに対応する整数を取得する。
errno_t Start(const ThreadSettings &settings, const FUNC &f)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:649
#define nlib_yield
スレッドの実行権を手放す。
errno_t SetPriority(int priority) noexcept
スレッドの優先度を設定します。優先度の値はプラットフォーム依存です。
Definition: Thread.h:78
時刻や時間を扱うためのクラスが定義されています。
nlib_thread_id GetId() noexcept
カレントスレッドのIDを取得します。
Definition: Thread.h:828
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
errno_t nlib_thread_attr_init(nlib_thread_attr *attr)
スレッド属性オブジェクトを初期化して、デフォルトに設定する。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:107
開発環境別の設定が書かれるファイルです。
errno_t nlib_sleep(nlib_duration t)
t の間スリープする。
Thread & operator=(Thread &&rhs) noexcept
ムーブ代入演算子です。C++11の利用時に有効です。
Definition: Thread.h:223
errno_t Start(const FUNC &f)
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:655
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:702
errno_t nlib_thread_setpriority(nlib_thread thread, int priority)
スレッドの実行優先度を設定します。数値の意味は実装依存です。
int nlib_thread_id
スレッド毎にユニークな整数値
Definition: Platform.h:1247
size_t GetHardwareConcurrency() noexcept
ハードウェアスレッドの数を返します。
Definition: Thread.h:814
errno_t SetAffinity(uint32_t affinity) noexcept
スレッドのプロセッサアフィニティマスクを設定します。
Definition: Thread.h:265
errno_t nlib_thread_join(nlib_thread thread)
スレッドの終了を待ちます。
ThreadArg< T1, T2, T3, T4, T5 > ThisType
この構造体の型へのtypedefです。
Definition: Thread.h:119
errno_t StartRaw(const ThreadSettings &settings, ThreadArg<>::Func func) noexcept
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:282
UniquePtr< ThisType > ArgType
スレッド関数の引数型のtypedefです。
Definition: Thread.h:120
errno_t nlib_thread_setname(nlib_thread thread, const char *name)
スレッドに名前をつけます。
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:613
errno_t nlib_thread_detach(nlib_thread thread)
実行中のスレッドをデタッチ状態にします。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
Thread(Thread &&rhs) noexcept
ムーブコンストラクタです。C++11の利用時に有効です。
Definition: Thread.h:220
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:717
errno_t nlib_thread_getcpu(int *result)
呼び出したスレッドが実行されているCPUを取得します。
errno_t nlib_thread_getpriority(nlib_thread thread, int *priority)
スレッドの現在の実行優先度を取得します。数値の意味は実装依存です。
時間を表すクラスです。
Definition: DateTime.h:97
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:581
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)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:631
errno_t SleepMilliSeconds(unsigned int millisec) noexcept
スリープします。
Definition: Thread.h:825
errno_t StartRaw(ThreadArg<>::Func func) noexcept
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:286
T4 arg4
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:131
errno_t GetPriority(int32_t *priority) noexcept
nlib_thread_getpriority()を呼び出して、スレッドの実行優先順位を取得します。
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:567
T2 arg2
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:129
pthread_t nlib_thread
スレッドを指し示す識別子
errno_t Start(const FUNC &f, move_tag)
引数を持たない関数を別スレッドで実行開始します。 引数を持たないこと以外は、引数が1つのバージョンと同...
Definition: Thread.h:560
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
引数を4つもつ関数を別スレッドで実行開始します。 引数を4つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:622
errno_t ChangePriority(int32_t priority) noexcept
nlib_thread_setpriority()を呼び出して、スレッドの実行優先順位を設定します。
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:673
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37