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_();
41  errno_t e =
42  nlib_thread_attr_setint(&attr_, NLIB_THREAD_ATTR_KEY_DETACHSTATE, detached ? 1 : 0);
43  NLIB_ASSERT_NOERR(e);
44  NLIB_UNUSED(e);
45  }
47  this->Init_();
48  int detached;
49  errno_t e = nlib_thread_attr_getint(&attr_, NLIB_THREAD_ATTR_KEY_DETACHSTATE, &detached);
50  NLIB_ASSERT_NOERR(e);
51  NLIB_UNUSED(e);
52  return (detached != 0);
53  }
55  this->Init_();
56  errno_t e = nlib_thread_attr_setint(&attr_, NLIB_THREAD_ATTR_KEY_STACKSIZE, size);
57  return e;
58  }
60  this->Init_();
61  int size;
62  errno_t e = nlib_thread_attr_getint(&attr_, NLIB_THREAD_ATTR_KEY_STACKSIZE, &size);
63  NLIB_ASSERT_NOERR(e);
64  NLIB_UNUSED(e);
65  return size;
66  }
68  this->Init_();
69  errno_t e = nlib_thread_attr_setint(&attr_, NLIB_THREAD_ATTR_KEY_PRIORITY, priority);
70  return e;
71  }
72  int GetPriority() const NLIB_NOEXCEPT {
73  this->Init_();
74  int priority;
75  errno_t e = nlib_thread_attr_getint(&attr_, NLIB_THREAD_ATTR_KEY_PRIORITY, &priority);
76  NLIB_ASSERT_NOERR(e);
77  NLIB_UNUSED(e);
78  return priority;
79  }
80  nlib_thread_attr* GetPtr() NLIB_NOEXCEPT { return is_initialized_ ? &attr_ : nullptr; }
81  const nlib_thread_attr* GetPtr() const NLIB_NOEXCEPT {
82  return is_initialized_ ? &attr_ : nullptr;
83  }
84 
85  private:
86  void Init_() const NLIB_NOEXCEPT {
87  if (!is_initialized_) {
88  errno_t e = nlib_thread_attr_init(&attr_);
89  NLIB_ASSERT_NOERR(e);
90  NLIB_UNUSED(e);
91  is_initialized_ = true;
92  }
93  }
94  mutable bool is_initialized_;
95  mutable nlib_thread_attr attr_;
96 };
97 
98 template<class T1 = None, class T2 = None, class T3 = None, class T4 = None, class T5 = None>
99 struct ThreadArg {
102  typedef void (*Func)(ArgType& ptr);
103  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2(), arg3(), arg4(), arg5() {}
104  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_) :
105  func(func_),
106  arg1(arg1_),
107  arg2(arg2_),
108  arg3(arg3_),
109  arg4(arg4_),
110  arg5(arg5_) {}
111  static void Call(ArgType& ptr) { ptr->func(ptr); }
112 
113  public:
114  Func func;
115  T1 arg1;
116  T2 arg2;
117  T3 arg3;
118  T4 arg4;
119  T5 arg5;
120 };
121 
122 template<>
123 struct ThreadArg<None, None, None, None, None> {
124  typedef ThreadArg<> ThisType;
125  typedef void ArgType;
126  typedef void (*Func)();
127 };
128 
129 template<class T1>
130 struct ThreadArg<T1, None, None, None, None> {
131  typedef ThreadArg<T1> ThisType;
132  typedef UniquePtr<ThisType> ArgType;
133  typedef void (*Func)(ArgType& ptr);
134  NLIB_CEXPR ThreadArg() : func(nullptr), arg1() {}
135  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_) : func(func_), arg1(arg1_) {}
136  static void Call(ArgType& ptr) { ptr->func(ptr); }
137 
138  public:
139  Func func;
140  T1 arg1;
141 };
142 
143 template<class T1, class T2>
144 struct ThreadArg<T1, T2, None, None, None> {
145  typedef ThreadArg<T1, T2> ThisType;
146  typedef UniquePtr<ThisType> ArgType;
147  typedef void (*Func)(ArgType& ptr);
148  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2() {}
149  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_) : func(func_), arg1(arg1_), arg2(arg2_) {}
150  static void Call(ArgType& ptr) { ptr->func(ptr); }
151 
152  public:
153  Func func;
154  T1 arg1;
155  T2 arg2;
156 };
157 
158 template<class T1, class T2, class T3>
159 struct ThreadArg<T1, T2, T3, None, None> {
160  typedef ThreadArg<T1, T2, T3> ThisType;
161  typedef UniquePtr<ThisType> ArgType;
162  typedef void (*Func)(ArgType& ptr);
163  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2(), arg3() {}
164  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_) :
165  func(func_),
166  arg1(arg1_),
167  arg2(arg2_),
168  arg3(arg3_) {}
169  static void Call(ArgType& ptr) { ptr->func(ptr); }
170 
171  public:
172  Func func;
173  T1 arg1;
174  T2 arg2;
175  T3 arg3;
176 };
177 
178 template<class T1, class T2, class T3, class T4>
179 struct ThreadArg<T1, T2, T3, T4, None> {
180  typedef ThreadArg<T1, T2, T3, T4> ThisType;
181  typedef UniquePtr<ThisType> ArgType;
182  typedef void (*Func)(ArgType& ptr);
183  NLIB_CEXPR ThreadArg() : func(nullptr), arg1(), arg2(), arg3(), arg4() {}
184  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_) :
185  func(func_),
186  arg1(arg1_),
187  arg2(arg2_),
188  arg3(arg3_),
189  arg4(arg4_) {}
190  static void Call(ArgType& ptr) { ptr->func(ptr); }
191 
192  public:
193  Func func;
194  T1 arg1;
195  T2 arg2;
196  T3 arg3;
197  T4 arg4;
198 };
199 
200 #ifdef _MSC_VER
201 #pragma warning(push)
202 #pragma warning(disable : 4180)
203 #endif
204 
205 // code snippets:
206 // Thread th;
207 // void func(...) {}
208 // th.Start(func, ...); # to create a thread and run func() on it
209 // th.Join(); # or th.Detach();
211  public:
212  typedef void (*ThreadFunc)(void* arg);
215  if (thread_id_ != NLIB_THREAD_INVALID) this->Join();
216  }
217 #ifdef __cpp_rvalue_references
218  Thread(Thread&& rhs) NLIB_NOEXCEPT : thread_id_(rhs.thread_id_) {
219  rhs.thread_id_ = NLIB_THREAD_INVALID;
220  }
222  thread_id_ = rhs.thread_id_;
223  rhs.thread_id_ = NLIB_THREAD_INVALID;
224  return *this;
225  }
226 #endif
227  Thread(Thread& rhs, move_tag) NLIB_NOEXCEPT : thread_id_(rhs.thread_id_) {
228  rhs.thread_id_ = NLIB_THREAD_INVALID;
229  }
231  thread_id_ = rhs.thread_id_;
232  rhs.thread_id_ = NLIB_THREAD_INVALID;
233  return *this;
234  }
235  errno_t StartRaw(ThreadFunc func, void* arg) NLIB_NOEXCEPT;
236  errno_t StartRaw(ThreadFunc func, void* arg, const ThreadSettings& settings) NLIB_NOEXCEPT;
238  if (!thread_id_) return ESRCH;
239  errno_t e = nlib_thread_join(thread_id_);
240  if (e == 0) {
241  thread_id_ = NLIB_THREAD_INVALID;
242  return 0;
243  } else {
244  return e;
245  }
246  }
248  errno_t e = nlib_thread_detach(thread_id_);
249  if (e == 0) thread_id_ = NLIB_THREAD_INVALID;
250  return e;
251  }
252  bool IsJoinable() const NLIB_NOEXCEPT { return thread_id_ != NLIB_THREAD_INVALID; }
253  errno_t GetPriority(int32_t* priority) NLIB_NOEXCEPT {
254  return nlib_thread_getpriority(this->GetNativeHandle(), reinterpret_cast<int*>(priority));
255  }
257  return nlib_thread_setpriority(this->GetNativeHandle(), priority);
258  }
259  errno_t SetAffinity(uint32_t affinity) NLIB_NOEXCEPT {
260  return nlib_thread_setaffinity(thread_id_, affinity);
261  }
262  nlib_thread GetNativeHandle() const NLIB_NOEXCEPT { return thread_id_; }
263  bool operator==(const Thread& rhs) NLIB_NOEXCEPT {
264  return nlib_thread_equal(thread_id_, rhs.thread_id_) ? true : false;
265  }
266  bool operator!=(const Thread& rhs) NLIB_NOEXCEPT {
267  return nlib_thread_equal(thread_id_, rhs.thread_id_) ? false : true;
268  }
269 
271  static errno_t Sleep(const TimeSpan& span) NLIB_NOEXCEPT {
272  return nlib_sleep(span.ToTimeValue().tick);
273  }
274 
275  public:
277  errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func), settings);
278  return e;
279  }
281  errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func));
282  return e;
283  }
284  template<class ThArg>
286  if (!ptr) return EINVAL;
287  errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>,
288  reinterpret_cast<void*>(ptr.get()), settings);
289  if (e == 0) ptr.release();
290  return e;
291  }
292  template<class ThArg>
294  if (!ptr) return EINVAL;
295  errno_t e =
296  this->StartRaw((ThreadFunc)Thread::Exec<ThArg>, reinterpret_cast<void*>(ptr.get()));
297  if (e == 0) ptr.release();
298  return e;
299  }
300 
301  private:
302  static void Exec(void* p) {
303  ThreadArg<>::Func f = reinterpret_cast<ThreadArg<>::Func>(p);
304  f();
305  }
306  template<class T>
307  static void Exec(void* p) {
308  UniquePtr<T> args(reinterpret_cast<T*>(p));
309  T::Call(args);
310  }
311 
312 #define NLIB_THFUNC_USESWAP_NO(tp) \
313  typename EnableIf<!IsSame<None, typename RemoveCv<B>::type>::value && \
314  (IsSame<FalseType, typename RemoveCv<B>::type>::value || \
315  !IsSwappable<tp>::value), \
316  const tp&>::type
317 #define NLIB_THFUNC_USESWAP_YES(tp) \
318  typename EnableIf<!IsSame<None, typename RemoveCv<B>::type>::value && \
319  IsSame<TrueType, typename RemoveCv<B>::type>::value && \
320  IsSwappable<tp>::value, \
321  const tp&>::type
322 
323  template<class T>
324  struct ArgType : public Conditional<IsArithmetic<T>::value || IsPointer<T>::value ||
325  IsMemberPointer<T>::value,
326  FalseType, TrueType> {};
327 
328  template<class FUNC>
329  struct Args0 {
330  typedef typename RemoveRef<FUNC>::type FUNC_;
331  template<class B>
332  Args0(NLIB_THFUNC_USESWAP_NO(FUNC_) func_, B) : func(func_) {}
333  template<class B>
334  Args0(NLIB_THFUNC_USESWAP_YES(FUNC_) func_, B) : func() {
335  using std::swap;
336  swap(const_cast<FUNC&>(func_), func);
337  }
338  template<class B>
339  Args0(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
340  func_,
341  B) :
342  func(func_) {}
343  FUNC_ func;
344 
345  private:
347  };
348  template<class FUNC, class T1>
349  struct Args1 : public Args0<FUNC> {
350  typedef Args0<FUNC> BaseType;
351  typedef typename RemoveRef<T1>::type T1_;
352  template<class B>
353  Args1(const FUNC& func_, NLIB_THFUNC_USESWAP_NO(T1) arg1_, B) :
354  BaseType(func_, typename IsSwappable<FUNC>::type()),
355  arg1(arg1_) {}
356  template<class B>
357  Args1(const FUNC& func_, NLIB_THFUNC_USESWAP_YES(T1) arg1_, B) :
358  BaseType(func_, typename IsSwappable<FUNC>::type()),
359  arg1() {
360  using std::swap;
361  swap(const_cast<T1&>(arg1_), arg1);
362  }
363  template<class B>
364  Args1(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
365  func_,
366  const T1& arg1_, B b) :
367  BaseType(func_, b),
368  arg1(arg1_) {}
369  T1_ arg1;
370  };
371  template<class FUNC, class T1, class T2>
372  struct Args2 : public Args1<FUNC, T1> {
373  typedef Args1<FUNC, T1> BaseType;
374  typedef typename RemoveRef<T2>::type T2_;
375  template<class B>
376  Args2(const FUNC& func_, const T1& arg1_, NLIB_THFUNC_USESWAP_NO(T2) arg2_, B) :
377  BaseType(func_, arg1_, typename ArgType<T1>::type()),
378  arg2(arg2_) {}
379  template<class B>
380  Args2(const FUNC& func_, const T1& arg1_, NLIB_THFUNC_USESWAP_YES(T2) arg2_, B) :
381  BaseType(func_, arg1_, typename ArgType<T1>::type()),
382  arg2() {
383  using std::swap;
384  swap(const_cast<T2&>(arg2_), arg2);
385  }
386  template<class B>
387  Args2(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
388  func_,
389  const T1& arg1_, const T2& arg2_, B b) :
390  BaseType(func_, arg1_, b),
391  arg2(arg2_) {}
392  T2_ arg2;
393  };
394  template<class FUNC, class T1, class T2, class T3>
395  struct Args3 : public Args2<FUNC, T1, T2> {
396  typedef Args2<FUNC, T1, T2> BaseType;
397  typedef typename RemoveRef<T3>::type T3_;
398  template<class B>
399  Args3(const FUNC& func_, const T1& arg1_, const T2& arg2_, NLIB_THFUNC_USESWAP_NO(T3) arg3_,
400  B) :
401  BaseType(func_, arg1_, arg2_, typename ArgType<T2>::type()),
402  arg3(arg3_) {}
403  template<class B>
404  Args3(const FUNC& func, const T1& arg1_, const T2& arg2_, NLIB_THFUNC_USESWAP_YES(T3) arg3_,
405  B) :
406  BaseType(func, arg1_, arg2_, typename ArgType<T2>::type()),
407  arg3() {
408  using std::swap;
409  swap(const_cast<T3&>(arg3_), arg3);
410  }
411  template<class B>
412  Args3(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
413  func_,
414  const T1& arg1_, const T2& arg2_, const T3& arg3_, B b) :
415  BaseType(func_, arg1_, arg2_, b),
416  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()),
427  arg4(arg4_) {}
428  template<class B>
429  Args4(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_,
430  NLIB_THFUNC_USESWAP_YES(T4) arg4_, B) :
431  BaseType(func_, arg1_, arg2_, arg3_, typename ArgType<T3>::type()),
432  arg4() {
433  using std::swap;
434  swap(const_cast<T4&>(arg4_), arg4);
435  }
436  template<class B>
437  Args4(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
438  func_,
439  const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_, B b) :
440  BaseType(func_, arg1_, arg2_, arg3_, b),
441  arg4(arg4_) {}
442  T4_ arg4;
443  };
444  template<class FUNC, class T1, class T2, class T3, class T4, class T5>
445  struct Args5 : public Args4<FUNC, T1, T2, T3, T4> {
446  typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
447  typedef typename RemoveRef<T5>::type T5_;
448  template<class B>
449  Args5(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_,
450  NLIB_THFUNC_USESWAP_NO(T5) arg5_, B) :
451  BaseType(func_, arg1_, arg2_, arg3_, arg4_, typename ArgType<T4>::type()),
452  arg5(arg5_) {}
453  template<class B>
454  Args5(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_,
455  NLIB_THFUNC_USESWAP_YES(T4) arg5_, B) :
456  BaseType(func_, arg1_, arg2_, arg3_, arg4_, typename ArgType<T4>::type()),
457  arg5() {
458  using std::swap;
459  swap(const_cast<T5&>(arg5_), arg5);
460  }
461  template<class B>
462  Args5(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
463  func_,
464  const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_, const T5& arg5_,
465  B b) :
466  BaseType(func_, arg1_, arg2_, arg3_, arg4_, b),
467  arg5(arg5_) {}
468  T5_ arg5;
469  };
470 #undef NLIB_THFUNC_USESWAP_NO
471 #undef NLIB_THFUNC_USESWAP_YES
472 
473  template<class FUNC>
474  struct ThreadFuncX0 : public Args0<FUNC> {
475  typedef ThreadFuncX0<FUNC> ThisType;
476  typedef Args0<FUNC> BaseType;
477  template<class B>
478  ThreadFuncX0(const FUNC& func_, B b) : BaseType(func_, b) {}
479  explicit ThreadFuncX0(const FUNC& func_) : BaseType(func_, None()) {}
480  static void Call(UniquePtr<ThisType>& ptr) { ptr->func(); }
481  };
482  template<class FUNC, class T1>
483  struct ThreadFuncX1 : public Args1<FUNC, T1> {
484  typedef ThreadFuncX1<FUNC, T1> ThisType;
485  typedef Args1<FUNC, T1> BaseType;
486  template<class B>
487  ThreadFuncX1(const FUNC& func_, const T1& arg1_, B b) : BaseType(func_, arg1_, b) {}
488  ThreadFuncX1(const FUNC& func_, const T1& arg1_) : BaseType(func_, arg1_, None()) {}
489  static void Call(UniquePtr<ThisType>& ptr) { ptr->func(NLIB_MOVE(ptr->arg1)); }
490  };
491  template<class FUNC, class T1, class T2>
492  struct ThreadFuncX2 : public Args2<FUNC, T1, T2> {
493  typedef ThreadFuncX2<FUNC, T1, T2> ThisType;
494  typedef Args2<FUNC, T1, T2> BaseType;
495  template<class B>
496  ThreadFuncX2(const FUNC& func_, const T1& arg1_, const T2& arg2_, B b) :
497  BaseType(func_, arg1_, arg2_, b) {}
498  ThreadFuncX2(const FUNC& func_, const T1& arg1_, const T2& arg2_) :
499  BaseType(func_, arg1_, arg2_, None()) {}
500  static void Call(UniquePtr<ThisType>& ptr) {
501  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2));
502  }
503  };
504  template<class FUNC, class T1, class T2, class T3>
505  struct ThreadFuncX3 : public Args3<FUNC, T1, T2, T3> {
506  typedef ThreadFuncX3<FUNC, T1, T2, T3> ThisType;
507  typedef Args3<FUNC, T1, T2, T3> BaseType;
508  template<class B>
509  ThreadFuncX3(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_, B b) :
510  BaseType(func_, arg1_, arg2_, arg3_, b) {}
511  ThreadFuncX3(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_) :
512  BaseType(func_, arg1_, arg2_, arg3_, None()) {}
513  static void Call(UniquePtr<ThisType>& ptr) {
514  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3));
515  }
516  };
517  template<class FUNC, class T1, class T2, class T3, class T4>
518  struct ThreadFuncX4 : public Args4<FUNC, T1, T2, T3, T4> {
519  typedef ThreadFuncX4<FUNC, T1, T2, T3, T4> ThisType;
520  typedef Args4<FUNC, T1, T2, T3, T4> BaseType;
521  template<class B>
522  ThreadFuncX4(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_,
523  const T4& arg4_, B b) :
524  BaseType(func_, arg1_, arg2_, arg3_, arg4_, b) {}
525  ThreadFuncX4(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_,
526  const T4& arg4_) :
527  BaseType(func_, arg1_, arg2_, arg3_, arg4_, None()) {}
528  static void Call(UniquePtr<ThisType>& ptr) {
529  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
530  NLIB_MOVE(ptr->arg4));
531  }
532  };
533  template<class FUNC, class T1, class T2, class T3, class T4, class T5>
534  struct ThreadFuncX5 : public Args5<FUNC, T1, T2, T3, T4, T5> {
535  typedef ThreadFuncX5<FUNC, T1, T2, T3, T4, T5> ThisType;
536  typedef Args5<FUNC, T1, T2, T3, T4, T5> BaseType;
537  template<class B>
538  ThreadFuncX5(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_,
539  const T4& arg4_, const T5& arg5_, B b) :
540  BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, b) {}
541  ThreadFuncX5(const FUNC& func_, const T1& arg1_, const T2& arg2_, const T3& arg3_,
542  const T4& arg4_, const T5& arg5_) :
543  BaseType(func_, arg1_, arg2_, arg3_, arg4_, arg5_, None()) {}
544  static void Call(UniquePtr<ThisType>& ptr) {
545  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
546  NLIB_MOVE(ptr->arg4), NLIB_MOVE(ptr->arg5));
547  }
548  };
549 
550  public:
551  template<class FUNC>
552  errno_t Start(const ThreadSettings& settings, const FUNC& f, move_tag) {
554  new (std::nothrow) ThreadFuncX0<FUNC>(f, typename IsSwappable<FUNC>::type()));
555  if (!ptr) return ENOMEM;
556  return this->StartRaw(settings, ptr);
557  }
558  template<class FUNC>
559  errno_t Start(const FUNC& f, move_tag) {
561  new (std::nothrow) ThreadFuncX0<FUNC>(f, typename IsSwappable<FUNC>::type()));
562  if (!ptr) return ENOMEM;
563  return this->StartRaw(ptr);
564  }
565  template<class FUNC, class T1>
566  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, move_tag) {
568  new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1, typename ArgType<T1>::type()));
569  if (!ptr) return ENOMEM;
570  return this->StartRaw(settings, ptr);
571  }
572  template<class FUNC, class T1>
573  errno_t Start(const FUNC& f, const T1& arg1, move_tag) {
575  new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1, typename ArgType<T1>::type()));
576  if (!ptr) return ENOMEM;
577  return this->StartRaw(ptr);
578  }
579  template<class FUNC, class T1, class T2>
580  errno_t
581  Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2, move_tag) {
582  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow) ThreadFuncX2<FUNC, T1, T2>(
583  f, arg1, arg2, typename ArgType<T2>::type()));
584  if (!ptr) return ENOMEM;
585  return this->StartRaw(settings, ptr);
586  }
587  template<class FUNC, class T1, class T2>
588  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, move_tag) {
589  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow) ThreadFuncX2<FUNC, T1, T2>(
590  f, arg1, arg2, typename ArgType<T2>::type()));
591  if (!ptr) return ENOMEM;
592  return this->StartRaw(ptr);
593  }
594  template<class FUNC, class T1, class T2, class T3>
595  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
596  const T3& arg3, move_tag) {
598  new (std::nothrow)
599  ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3, typename ArgType<T3>::type()));
600  if (!ptr) return ENOMEM;
601  return this->StartRaw(settings, ptr);
602  }
603  template<class FUNC, class T1, class T2, class T3>
604  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, move_tag) {
606  new (std::nothrow)
607  ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3, typename ArgType<T3>::type()));
608  if (!ptr) return ENOMEM;
609  return this->StartRaw(ptr);
610  }
611  template<class FUNC, class T1, class T2, class T3, class T4>
612  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
613  const T3& arg3, const T4& arg4, move_tag) {
615  new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
616  typename ArgType<T4>::type()));
617  if (!ptr) return ENOMEM;
618  return this->StartRaw(settings, ptr);
619  }
620  template<class FUNC, class T1, class T2, class T3, class T4>
621  errno_t
622  Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4, move_tag) {
624  new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4,
625  typename ArgType<T4>::type()));
626  if (!ptr) return ENOMEM;
627  return this->StartRaw(ptr);
628  }
629  template<class FUNC, class T1, class T2, class T3, class T4, class T5>
630  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
631  const T3& arg3, const T4& arg4, const T5& arg5, move_tag) {
633  new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
634  f, arg1, arg2, arg3, arg4, arg5, typename ArgType<T5>::type()));
635  if (!ptr) return ENOMEM;
636  return this->StartRaw(settings, ptr);
637  }
638  template<class FUNC, class T1, class T2, class T3, class T4, class T5>
639  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4,
640  const T5& arg5, move_tag) {
642  new (std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(
643  f, arg1, arg2, arg3, arg4, arg5, typename ArgType<T5>::type()));
644  if (!ptr) return ENOMEM;
645  return this->StartRaw(ptr);
646  }
647  template<class FUNC>
648  errno_t Start(const ThreadSettings& settings, const FUNC& f) {
649  UniquePtr<ThreadFuncX0<FUNC> > ptr(new (std::nothrow) ThreadFuncX0<FUNC>(f));
650  if (!ptr) return ENOMEM;
651  return this->StartRaw(settings, ptr);
652  }
653  template<class FUNC>
654  errno_t Start(const FUNC& f) {
655  UniquePtr<ThreadFuncX0<FUNC> > ptr(new (std::nothrow) ThreadFuncX0<FUNC>(f));
656  if (!ptr) return ENOMEM;
657  return this->StartRaw(ptr);
658  }
659  template<class FUNC, class T1>
660  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1) {
661  UniquePtr<ThreadFuncX1<FUNC, T1> > ptr(new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1));
662  if (!ptr) return ENOMEM;
663  return this->StartRaw(settings, ptr);
664  }
665  template<class FUNC, class T1>
666  errno_t Start(const FUNC& f, const T1& arg1) {
667  UniquePtr<ThreadFuncX1<FUNC, T1> > ptr(new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1));
668  if (!ptr) return ENOMEM;
669  return this->StartRaw(ptr);
670  }
671  template<class FUNC, class T1, class T2>
672  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2) {
673  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow)
674  ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
675  if (!ptr) return ENOMEM;
676  return this->StartRaw(settings, ptr);
677  }
678  template<class FUNC, class T1, class T2>
679  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2) {
680  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow)
681  ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
682  if (!ptr) return ENOMEM;
683  return this->StartRaw(ptr);
684  }
685  template<class FUNC, class T1, class T2, class T3>
686  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
687  const T3& arg3) {
689  new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
690  if (!ptr) return ENOMEM;
691  return this->StartRaw(settings, ptr);
692  }
693  template<class FUNC, class T1, class T2, class T3>
694  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3) {
696  new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
697  if (!ptr) return ENOMEM;
698  return this->StartRaw(ptr);
699  }
700  template<class FUNC, class T1, class T2, class T3, class T4>
701  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
702  const T3& arg3, const T4& arg4) {
704  new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
705  if (!ptr) return ENOMEM;
706  return this->StartRaw(settings, ptr);
707  }
708  template<class FUNC, class T1, class T2, class T3, class T4>
709  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4) {
711  new (std::nothrow) ThreadFuncX4<FUNC, T1, T2, T3, T4>(f, arg1, arg2, arg3, arg4));
712  if (!ptr) return ENOMEM;
713  return this->StartRaw(ptr);
714  }
715  template<class FUNC, class T1, class T2, class T3, class T4, class T5>
716  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
717  const T3& arg3, const T4& arg4, const T5& arg5) {
719  std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
720  if (!ptr) return ENOMEM;
721  return this->StartRaw(settings, ptr);
722  }
723  template<class FUNC, class T1, class T2, class T3, class T4, class T5>
724  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4,
725  const T5& arg5) {
727  std::nothrow) ThreadFuncX5<FUNC, T1, T2, T3, T4, T5>(f, arg1, arg2, arg3, arg4, arg5));
728  if (!ptr) return ENOMEM;
729  return this->StartRaw(ptr);
730  }
731 
732  private:
733  nlib_thread thread_id_;
735 };
736 
737 #ifdef _MSC_VER
738 #pragma warning(pop)
739 #endif
740 
741 template<>
742 struct Thread::Args0<void()> {
743  template<class X>
744  Args0(void (*func_)(), X) : func(func_) {}
745  Args0(void (*func_)()) : func(func_) {}
746  void (*func)();
747 
748  private:
750 };
751 
752 template<class T1>
753 struct Thread::Args0<void(T1)> {
754  template<class X>
755  Args0(void (*func_)(T1), X) : func(func_) {}
756  Args0(void (*func_)(T1)) : func(func_) {}
757  void (*func)(T1);
758 
759  private:
761 };
762 
763 template<class T1, class T2>
764 struct Thread::Args0<void(T1, T2)> {
765  template<class X>
766  Args0(void (*func_)(T1, T2), X) : func(func_) {}
767  Args0(void (*func_)(T1, T2)) : func(func_) {}
768  void (*func)(T1, T2);
769 
770  private:
772 };
773 
774 template<class T1, class T2, class T3>
775 struct Thread::Args0<void(T1, T2, T3)> {
776  template<class X>
777  Args0(void (*func_)(T1, T2, T3), X) : func(func_) {}
778  Args0(void (*func_)(T1, T2, T3)) : func(func_) {}
779  void (*func)(T1, T2, T3);
780 
781  private:
783 };
784 
785 template<class T1, class T2, class T3, class T4>
786 struct Thread::Args0<void(T1, T2, T3, T4)> {
787  template<class X>
788  Args0(void (*func_)(T1, T2, T3, T4), X) : func(func_) {}
789  Args0(void (*func_)(T1, T2, T3, T4)) : func(func_) {}
790  void (*func)(T1, T2, T3, T4);
791 
792  private:
794 };
795 
796 template<class T1, class T2, class T3, class T4, class T5>
797 struct Thread::Args0<void(T1, T2, T3, T4, T5)> {
798  template<class X>
799  Args0(void (*func_)(T1, T2, T3, T4, T5), X) : func(func_) {}
800  Args0(void (*func_)(T1, T2, T3, T4, T5)) : func(func_) {}
801  void (*func)(T1, T2, T3, T4, T5);
802 
803  private:
805 };
806 
808  unsigned int num_cpu;
809  (void)nlib_thread_getconcurrency(&num_cpu);
810  return num_cpu;
811 }
812 
813 namespace this_thread {
815  return nlib_yield();
816 }
817 inline errno_t Sleep(const TimeSpan& span) NLIB_NOEXCEPT {
819 }
820 inline errno_t SleepMilliSeconds(unsigned int millisec) NLIB_NOEXCEPT {
821  return Sleep(TimeSpan(0, 0, millisec));
822 }
824  nlib_thread_id id;
825  errno_t e = nlib_thread_getid(&id);
826  return e == 0 ? id : -1;
827 }
828 inline errno_t GetCpu(int* cpuid) NLIB_NOEXCEPT {
829  return nlib_thread_getcpu(cpuid);
830 }
831 inline errno_t SetName(const char* literal_string) NLIB_NOEXCEPT {
832  nlib_thread th;
833  errno_t e = nlib_thread_self(&th);
834  if (e != 0) return e;
835  return nlib_thread_setname(th, literal_string);
836 }
839 
840 } // namespace this_thread
841 
842 } // namespace threading
843 NLIB_NAMESPACE_END
844 
845 NLIB_DEFINE_STD_SWAP(::nlib_ns::threading::Thread)
846 
847 #endif // INCLUDE_NN_NLIB_THREADING_THREAD_H_
errno_t GetCpu(int *cpuid) noexcept
呼び出したスレッドが実行されているCPUを取得します。
Definition: Thread.h:828
errno_t YieldThread() noexcept
他のスレッドに制御を譲ります。
Definition: Thread.h:814
errno_t Start(const ThreadSettings &settings, const FUNC &f, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:552
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
引数を2つもつ関数を別スレッドで実行開始します。 引数を2つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:588
nlib_thread GetNativeHandle() const noexcept
実装依存のスレッド識別子を取得します。
Definition: Thread.h:262
static errno_t Sleep(const TimeSpan &span) noexcept
span の期間だけスリープします。
Definition: Thread.h:271
int nlib_thread_equal(nlib_thread th1, nlib_thread th2)
2つのスレッドが同一スレッドを指すかどうかチェックします。
constexpr Thread() noexcept
デフォルトコンストラクタです。
Definition: Thread.h:213
int GetStackSize() const noexcept
設定されているスタックサイズを返します。
Definition: Thread.h:59
errno_t Sleep(const TimeSpan &span) noexcept
スリープします。
Definition: Thread.h:817
constexpr ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_)
コンストラクタで構造体のフィールドを初期化します。
Definition: Thread.h:104
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:686
bool operator!=(const Thread &rhs) noexcept
nlib_thread_equal()を用いてスレッドを比較します。
Definition: Thread.h:266
constexpr ThreadArg()
デフォルトコンストラクタです。
Definition: Thread.h:103
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:183
T1 arg1
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:115
errno_t Detach() noexcept
スレッドをデタッチします。
Definition: Thread.h:247
Func func
別スレッドで実行する関数へのポインタです。
Definition: Thread.h:114
bool GetDetachState() const noexcept
デタッチ状態でスレッドを起動する設定かどうかを返します。
Definition: Thread.h:46
errno_t nlib_thread_setaffinity(nlib_thread thread, uint32_t affinity)
指定されたスレッドのプロセッサアフィニティマスクを設定します。
errno_t GetPriority(int32_t *priority) noexcept
スレッドの優先度を取得します。
Definition: Thread.h:253
T3 arg3
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:117
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:639
~Thread() noexcept
デストラクタです。 スレッドがJoinされていない場合はJoinします。
Definition: Thread.h:214
struct nlib_thread_attr_ nlib_thread_attr
新しく作られるスレッドに適用されるスレッド属性
Definition: Platform.h:1233
errno_t ChangePriority(int32_t priority) noexcept
スレッドの優先度を設定します。
Definition: Thread.h:256
T5 arg5
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:119
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:679
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
引数を3つもつ関数を別スレッドで実行開始します。引数を3つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:694
C++11環境(エイリアステンプレートが可能な環境)においてはstd::unique_ptrにエイリアステンプレートされま...
Definition: UniquePtr.h:108
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:724
std::unique_ptrに相当するクラスが定義されています。
スレッドの生成・開始を行うためのクラスです。
Definition: Thread.h:210
errno_t nlib_thread_getid(nlib_thread_id *id)
実行中のスレッドに対応する一意の整数値を格納する。
Threadクラスでスレッドを実行するために利用できる構造体です。
Definition: Thread.h:99
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
引数を3つもつ関数を別スレッドで実行開始します。 引数を3つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:604
errno_t nlib_thread_attr_setint(nlib_thread_attr *attr, int key, int value)
スレッドの属性オブジェクトのキーに対応する整数を設定する。
int GetPriority() const noexcept
スレッドの優先度を取得します。
Definition: Thread.h:72
bool IsJoinable() const noexcept
join可能かどうか調べます。
Definition: Thread.h:252
Thread(Thread &rhs, move_tag) noexcept
ムーブコンストラクタに相当します。
Definition: Thread.h:227
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
errno_t nlib_thread_getconcurrency(unsigned int *num_cpu)
ハードウェアスレッドの数を取得します。
errno_t SetStackSize(int size) noexcept
スタックサイズを設定します。
Definition: Thread.h:54
errno_t Start(const FUNC &f, const T1 &arg1, move_tag)
引数を1つもつ関数を別スレッドで実行開始します。
Definition: Thread.h:573
errno_t StartRaw(UniquePtr< ThArg > &ptr) noexcept
別スレッドの実行を開始します。
Definition: Thread.h:293
errno_t SetName(const char *literal_string) noexcept
スレッドに名前をつけます。
Definition: Thread.h:831
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:285
Thread & assign(Thread &rhs, move_tag) noexcept
ムーブ代入演算子に相当します。
Definition: Thread.h:230
static errno_t YieldThread() noexcept
nlib_yield()を呼び出します。
Definition: Thread.h:270
errno_t Join() noexcept
スレッドの実行完了を待ちます。
Definition: Thread.h:237
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:595
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:270
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:660
bool operator==(const Thread &rhs) noexcept
nlib_thread_equal()を用いてスレッドを比較します。
Definition: Thread.h:263
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:709
errno_t Start(const FUNC &f, const T1 &arg1)
引数を1つもつ関数を別スレッドで実行開始します。
Definition: Thread.h:666
#define NLIB_THREAD_INVALID
無効なスレッドを指し示す値
Definition: Platform.h:1217
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:648
#define nlib_yield
スレッドの実行権を手放す。
errno_t SetPriority(int priority) noexcept
スレッドの優先度を設定します。優先度の値はプラットフォーム依存です。
Definition: Thread.h:67
時刻や時間を扱うためのクラスが定義されています。
nlib_thread_id GetId() noexcept
カレントスレッドのIDを取得します。
Definition: Thread.h:823
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
errno_t nlib_thread_attr_init(nlib_thread_attr *attr)
スレッド属性オブジェクトを初期化して、デフォルトに設定する。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:111
開発環境別の設定が書かれるファイルです。
errno_t nlib_sleep(nlib_duration t)
t の間スリープする。
Thread & operator=(Thread &&rhs) noexcept
ムーブ代入演算子です。
Definition: Thread.h:221
errno_t Start(const FUNC &f)
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:654
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:701
errno_t nlib_thread_setpriority(nlib_thread thread, int priority)
スレッドの実行優先度を設定します。数値の意味は実装依存です。
int nlib_thread_id
スレッド毎にユニークな整数値
Definition: Platform.h:1243
size_t GetHardwareConcurrency() noexcept
ハードウェアスレッドの数を返します。
Definition: Thread.h:807
errno_t SetAffinity(uint32_t affinity) noexcept
スレッドのプロセッサアフィニティマスクを設定します。
Definition: Thread.h:259
errno_t nlib_thread_join(nlib_thread thread)
スレッドの終了を待ちます。
ThreadArg< T1, T2, T3, T4, T5 > ThisType
この構造体の型へのtypedefです。
Definition: Thread.h:100
errno_t StartRaw(const ThreadSettings &settings, ThreadArg<>::Func func) noexcept
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:276
UniquePtr< ThisType > ArgType
スレッド関数の引数型のtypedefです。
Definition: Thread.h:101
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:612
errno_t nlib_thread_detach(nlib_thread thread)
実行中のスレッドをデタッチ状態にします。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:250
Thread(Thread &&rhs) noexcept
ムーブコンストラクタです。
Definition: Thread.h:218
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:716
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:630
errno_t SleepMilliSeconds(unsigned int millisec) noexcept
スリープします。
Definition: Thread.h:820
errno_t StartRaw(ThreadArg<>::Func func) noexcept
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:280
T4 arg4
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:118
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:566
T2 arg2
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:116
pthread_t nlib_thread
スレッドを指し示す識別子
errno_t Start(const FUNC &f, move_tag)
引数を持たない関数を別スレッドで実行開始します。 引数を持たないこと以外は、引数が1つのバージョンと同...
Definition: Thread.h:559
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:672
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37