nlib
Thread.h
[詳解]
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_THREAD_H_
4 #define INCLUDE_NN_NLIB_THREADING_THREAD_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/Swap.h"
8 #include "nn/nlib/UniquePtr.h"
9 #include "nn/nlib/TypeTraits.h"
10 #include "nn/nlib/DateTime.h"
11 
12 NLIB_NAMESPACE_BEGIN
13 namespace threading {
14 struct None {};
15 
17  public:
18  ThreadSettings() NLIB_NOEXCEPT : is_initialized_(false) {}
20  if (is_initialized_) {
22  NLIB_ASSERT_NOERR(e);
23  NLIB_UNUSED(e);
24  }
25  }
26  void SetDetachState(bool detached) NLIB_NOEXCEPT {
27  this->Init_();
29  &attr_,
30  NLIB_THREAD_ATTR_KEY_DETACHSTATE,
31  detached ? 1 : 0);
32  NLIB_ASSERT_NOERR(e);
33  NLIB_UNUSED(e);
34  }
36  this->Init_();
37  int detached;
39  &attr_,
40  NLIB_THREAD_ATTR_KEY_DETACHSTATE,
41  &detached);
42  NLIB_ASSERT_NOERR(e);
43  NLIB_UNUSED(e);
44  return (detached != 0);
45  }
47  this->Init_();
49  &attr_,
50  NLIB_THREAD_ATTR_KEY_STACKSIZE,
51  size);
52  return e;
53  }
55  this->Init_();
56  int size;
58  &attr_,
59  NLIB_THREAD_ATTR_KEY_STACKSIZE,
60  &size);
61  NLIB_ASSERT_NOERR(e);
62  NLIB_UNUSED(e);
63  return size;
64  }
66  this->Init_();
68  &attr_,
69  NLIB_THREAD_ATTR_KEY_PRIORITY,
70  priority);
71  return e;
72  }
73  int GetPriority() const NLIB_NOEXCEPT {
74  this->Init_();
75  int priority;
77  &attr_,
78  NLIB_THREAD_ATTR_KEY_PRIORITY,
79  &priority);
80  NLIB_ASSERT_NOERR(e);
81  NLIB_UNUSED(e);
82  return priority;
83  }
85  return is_initialized_ ? &attr_ : NULL;
86  }
87  const nlib_thread_attr* GetPtr() const NLIB_NOEXCEPT {
88  return is_initialized_ ? &attr_ : NULL;
89  }
90 
91  private:
92  void Init_() const NLIB_NOEXCEPT {
93  if (!is_initialized_) {
94  errno_t e = nlib_thread_attr_init(&attr_);
95  NLIB_ASSERT_NOERR(e);
96  NLIB_UNUSED(e);
97  is_initialized_ = true;
98  }
99  }
100  mutable bool is_initialized_;
101  mutable nlib_thread_attr attr_;
102 };
103 
104 template <class T1 = None, class T2 = None, class T3 = None, class T4 = None, class T5 = None>
105 struct ThreadArg {
108  typedef void (*Func)(ArgType& ptr); // NOLINT
109  NLIB_CEXPR ThreadArg() : func(NULL), arg1(), arg2(), arg3(), arg4(), arg5() {}
110  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_)
111  : func(func_), arg1(arg1_), arg2(arg2_), arg3(arg3_), arg4(arg4_), arg5(arg5_) {}
112  static void Call(ArgType& ptr) { ptr->func(ptr); } // NOLINT
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;
133  typedef void (*Func)(ArgType& ptr); // NOLINT
134  NLIB_CEXPR ThreadArg() : func(NULL), arg1() {}
135  NLIB_CEXPR ThreadArg(Func func_, T1 arg1_) : func(func_), arg1(arg1_) {}
136  static void Call(ArgType& ptr) { ptr->func(ptr); } // NOLINT
137  public:
138  Func func;
139  T1 arg1;
140 };
141 
142 template <class T1, class T2>
143 struct ThreadArg<T1, T2, None, None, None> {
144  typedef ThreadArg<T1, T2> ThisType;
146  typedef void (*Func)(ArgType& ptr); // NOLINT
147  NLIB_CEXPR ThreadArg() : func(NULL), arg1(), arg2() {}
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); } // NOLINT
150  public:
151  Func func;
152  T1 arg1;
153  T2 arg2;
154 };
155 
156 template <class T1, class T2, class T3>
157 struct ThreadArg<T1, T2, T3, None, None> {
160  typedef void (*Func)(ArgType& ptr); // NOLINT
161  NLIB_CEXPR ThreadArg() : func(NULL), arg1(), arg2(), arg3() {}
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); } // NOLINT
165  public:
166  Func func;
167  T1 arg1;
168  T2 arg2;
169  T3 arg3;
170 };
171 
172 template <class T1, class T2, class T3, class T4>
173 struct ThreadArg<T1, T2, T3, T4, None> {
176  typedef void (*Func)(ArgType& ptr); // NOLINT
177  NLIB_CEXPR ThreadArg() : func(NULL), arg1(), arg2(), arg3(), arg4() {}
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); } // NOLINT
181  public:
182  Func func;
183  T1 arg1;
184  T2 arg2;
185  T3 arg3;
186  T4 arg4;
187 };
188 
189 #ifdef _MSC_VER
190 #pragma warning(push)
191 #pragma warning(disable : 4180)
192 #endif
193 
194 // code snippets:
195 // Thread th;
196 // void func(...) {}
197 // th.Start(func, ...); # to create a thread and run func() on it
198 // th.Join(); # or th.Detach();
200  public:
201  typedef void (*ThreadFunc)(void* arg);
204  if (thread_id_ != NLIB_THREAD_INVALID) this->Join();
205  }
206  NLIB_MOVE_MEMBER_HELPER_1(Thread, thread_id_)
207  errno_t StartRaw(ThreadFunc func, void* arg) NLIB_NOEXCEPT;
208  errno_t StartRaw(ThreadFunc func, void* arg, const ThreadSettings& settings) NLIB_NOEXCEPT;
209  errno_t Join() NLIB_NOEXCEPT {
210  if (!thread_id_) return ESRCH;
211  errno_t e = nlib_thread_join(thread_id_);
212  if (e == 0) {
213  thread_id_ = NLIB_THREAD_INVALID;
214  return 0;
215  } else {
216  return e;
217  }
218  }
219  errno_t Detach() NLIB_NOEXCEPT {
220  errno_t e = nlib_thread_detach(thread_id_);
221  if (e == 0) thread_id_ = NLIB_THREAD_INVALID;
222  return e;
223  }
224  bool IsJoinable() const NLIB_NOEXCEPT { return thread_id_ != NLIB_THREAD_INVALID; }
225  errno_t GetPriority(int32_t* priority) NLIB_NOEXCEPT {
226  return nlib_thread_getpriority(this->GetNativeHandle(), reinterpret_cast<int*>(priority));
227  }
228  errno_t ChangePriority(int32_t priority) NLIB_NOEXCEPT {
229  return nlib_thread_setpriority(this->GetNativeHandle(), priority);
230  }
231  errno_t SetAffinity(uint32_t affinity) NLIB_NOEXCEPT {
232  return nlib_thread_setaffinity(thread_id_, affinity);
233  }
234  nlib_thread GetNativeHandle() const NLIB_NOEXCEPT { return thread_id_; }
235  void swap(Thread& rhs) NLIB_NOEXCEPT { // NOLINT
236  using std::swap;
237  swap(thread_id_, rhs.thread_id_);
238  }
239  bool operator==(const Thread& rhs) NLIB_NOEXCEPT {
240  return nlib_thread_equal(thread_id_, rhs.thread_id_) ? true : false;
241  }
242  bool operator!=(const Thread& rhs) NLIB_NOEXCEPT {
243  return nlib_thread_equal(thread_id_, rhs.thread_id_) ? false : true;
244  }
245 
246  static errno_t YieldThread() NLIB_NOEXCEPT { return nlib_yield(); }
247  static errno_t Sleep(const TimeSpan& span) NLIB_NOEXCEPT {
248  return nlib_sleep(span.ToTimeValue().tick);
249  }
250 
251  public:
252  errno_t StartRaw(const ThreadSettings& settings, ThreadArg<>::Func func) NLIB_NOEXCEPT {
253  errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func), settings);
254  return e;
255  }
256  errno_t StartRaw(ThreadArg<>::Func func) NLIB_NOEXCEPT {
257  errno_t e = this->StartRaw(Thread::Exec, reinterpret_cast<void*>(func));
258  return e;
259  }
260  template <class ThArg>
261  errno_t StartRaw(const ThreadSettings& settings,
262  UniquePtr<ThArg>& ptr) NLIB_NOEXCEPT { // NOLINT
263  if (!ptr) return EINVAL;
264  errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>, // NOLINT
265  reinterpret_cast<void*>(ptr.get()), settings);
266  if (e == 0) ptr.release();
267  return e;
268  }
269  template <class ThArg>
270  errno_t StartRaw(UniquePtr<ThArg>& ptr) NLIB_NOEXCEPT { // NOLINT
271  if (!ptr) return EINVAL;
272  errno_t e = this->StartRaw((ThreadFunc)Thread::Exec<ThArg>, // NOLINT
273  reinterpret_cast<void*>(ptr.get()));
274  if (e == 0) ptr.release();
275  return e;
276  }
277 
278  private:
279  static void Exec(void* p) {
280  ThreadArg<>::Func f = reinterpret_cast<ThreadArg<>::Func>(p);
281  f();
282  }
283  template <class T>
284  static void Exec(void* p) {
285  UniquePtr<T> args(reinterpret_cast<T*>(p));
286  T::Call(args);
287  }
288 
289 #define NLIB_THFUNC_USESWAP_NO(tp) \
290  typename EnableIf< \
291  !IsSame<None, typename RemoveCv<B>::type>::value && \
292  (IsSame<FalseType, typename RemoveCv<B>::type>::value || !IsSwappable<tp>::value), \
293  const tp&>::type
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, \
298  const tp&>::type
299 
300  template <class T>
301  struct ArgType : public Conditional<
302  IsArithmetic<T>::value || IsPointer<T>::value || IsMemberPointer<T>::value,
303  FalseType, TrueType> {};
304 
305  template <class FUNC>
306  struct Args0 {
307  typedef typename RemoveRef<FUNC>::type FUNC_;
308  template <class B>
309  Args0(NLIB_THFUNC_USESWAP_NO(FUNC_) func_, B)
310  : func(func_) {}
311  template <class B>
312  Args0(NLIB_THFUNC_USESWAP_YES(FUNC_) func_, B)
313  : func() {
314  using std::swap;
315  swap(const_cast<FUNC&>(func_), func);
316  }
317  template <class B>
318  Args0(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
319  func_,
320  B)
321  : func(func_) {}
322  FUNC_ func;
323 
324  private:
326  };
327  template <class FUNC, class T1>
328  struct Args1 : public Args0<FUNC> {
329  typedef Args0<FUNC> BaseType;
330  typedef typename RemoveRef<T1>::type T1_;
331  template <class B>
332  Args1(const FUNC& func_, NLIB_THFUNC_USESWAP_NO(T1) arg1_, B)
333  : BaseType(func_, typename IsSwappable<FUNC>::type()), arg1(arg1_) {}
334  template <class B>
335  Args1(const FUNC& func_, NLIB_THFUNC_USESWAP_YES(T1) arg1_, B)
336  : BaseType(func_, typename IsSwappable<FUNC>::type()), arg1() {
337  using std::swap;
338  swap(const_cast<T1&>(arg1_), arg1);
339  }
340  template <class B>
341  Args1(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
342  func_,
343  const T1& arg1_, B b)
344  : BaseType(func_, b), arg1(arg1_) {}
345  T1_ arg1;
346  };
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_;
351  template <class B>
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_) {}
354  template <class B>
355  Args2(const FUNC& func_, const T1& arg1_, NLIB_THFUNC_USESWAP_YES(T2) arg2_, B)
356  : BaseType(func_, arg1_, typename ArgType<T1>::type()), arg2() {
357  using std::swap;
358  swap(const_cast<T2&>(arg2_), arg2);
359  }
360  template <class B>
361  Args2(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
362  func_,
363  const T1& arg1_, const T2& arg2_, B b)
364  : BaseType(func_, arg1_, b), arg2(arg2_) {}
365  T2_ arg2;
366  };
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_;
371  template <class B>
372  Args3(const FUNC& func_, const T1& arg1_, const T2& arg2_, NLIB_THFUNC_USESWAP_NO(T3) arg3_,
373  B)
374  : BaseType(func_, arg1_, arg2_, typename ArgType<T2>::type()), arg3(arg3_) {}
375  template <class B>
376  Args3(const FUNC& func, const T1& arg1_, const T2& arg2_, NLIB_THFUNC_USESWAP_YES(T3) arg3_,
377  B)
378  : BaseType(func, arg1_, arg2_, typename ArgType<T2>::type()), arg3() {
379  using std::swap;
380  swap(const_cast<T3&>(arg3_), arg3);
381  }
382  template <class B>
383  Args3(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
384  func_,
385  const T1& arg1_, const T2& arg2_, const T3& arg3_, B b)
386  : BaseType(func_, arg1_, arg2_, b), arg3(arg3_) {}
387  T3_ arg3;
388  };
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_;
393  template <class B>
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_) {}
397  template <class B>
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() {
401  using std::swap;
402  swap(const_cast<T4&>(arg4_), arg4);
403  }
404  template <class B>
405  Args4(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
406  func_,
407  const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_, B b)
408  : BaseType(func_, arg1_, arg2_, arg3_, b), arg4(arg4_) {}
409  T4_ arg4;
410  };
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_;
415  template <class B>
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()),
419  arg5(arg5_) {}
420  template <class B>
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() {
424  using std::swap;
425  swap(const_cast<T5&>(arg5_), arg5);
426  }
427  template <class B>
428  Args5(typename EnableIf<IsSame<typename RemoveCv<B>::type, None>::value, const FUNC&>::type
429  func_,
430  const T1& arg1_, const T2& arg2_, const T3& arg3_, const T4& arg4_, const T5& arg5_,
431  B b)
432  : BaseType(func_, arg1_, arg2_, arg3_, arg4_, b), arg5(arg5_) {}
433  T5_ arg5;
434  };
435 #undef NLIB_THFUNC_USESWAP_NO
436 #undef NLIB_THFUNC_USESWAP_YES
437 
438  template <class FUNC>
439  struct ThreadFuncX0 : public Args0<FUNC> {
440  typedef ThreadFuncX0<FUNC> ThisType;
441  typedef Args0<FUNC> BaseType;
442  template <class B>
443  ThreadFuncX0(const FUNC& func_, B b)
444  : BaseType(func_, b) {}
445  explicit ThreadFuncX0(const FUNC& func_) : BaseType(func_, None()) {}
446  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
447  ptr->func();
448  }
449  };
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;
454  template <class B>
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()) {}
458  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
459  ptr->func(NLIB_MOVE(ptr->arg1));
460  }
461  };
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;
466  template <class B>
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()) {}
471  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
472  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2));
473  }
474  };
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;
479  template <class B>
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()) {}
484  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
485  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3));
486  }
487  };
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;
492  template <class B>
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_,
497  const T4& arg4_)
498  : BaseType(func_, arg1_, arg2_, arg3_, arg4_, None()) {}
499  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
500  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
501  NLIB_MOVE(ptr->arg4));
502  }
503  };
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;
508  template <class B>
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()) {}
515  static void Call(UniquePtr<ThisType>& ptr) { // NOLINT
516  ptr->func(NLIB_MOVE(ptr->arg1), NLIB_MOVE(ptr->arg2), NLIB_MOVE(ptr->arg3),
517  NLIB_MOVE(ptr->arg4), NLIB_MOVE(ptr->arg5));
518  }
519  };
520 
521  public:
522  template <class FUNC>
523  errno_t Start(const ThreadSettings& settings, const FUNC& f, move_tag) {
525  new (std::nothrow) ThreadFuncX0<FUNC>(f, typename IsSwappable<FUNC>::type()));
526  if (!ptr) return ENOMEM;
527  return this->StartRaw(settings, ptr);
528  }
529  template <class FUNC>
530  errno_t Start(const FUNC& f, move_tag) {
532  new (std::nothrow) ThreadFuncX0<FUNC>(f, typename IsSwappable<FUNC>::type()));
533  if (!ptr) return ENOMEM;
534  return this->StartRaw(ptr);
535  }
536  template <class FUNC, class T1>
537  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, move_tag) {
539  new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1, typename ArgType<T1>::type()));
540  if (!ptr) return ENOMEM;
541  return this->StartRaw(settings, ptr);
542  }
543  template <class FUNC, class T1>
544  errno_t Start(const FUNC& f, const T1& arg1, move_tag) {
546  new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1, typename ArgType<T1>::type()));
547  if (!ptr) return ENOMEM;
548  return this->StartRaw(ptr);
549  }
550  template <class FUNC, class T1, class T2>
551  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
552  move_tag) {
553  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow) ThreadFuncX2<FUNC, T1, T2>(
554  f, arg1, arg2, typename ArgType<T2>::type()));
555  if (!ptr) return ENOMEM;
556  return this->StartRaw(settings, ptr);
557  }
558  template <class FUNC, class T1, class T2>
559  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, move_tag) {
560  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow) ThreadFuncX2<FUNC, T1, T2>(
561  f, arg1, arg2, typename ArgType<T2>::type()));
562  if (!ptr) return ENOMEM;
563  return this->StartRaw(ptr);
564  }
565  template <class FUNC, class T1, class T2, class T3>
566  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
567  const T3& arg3, move_tag) {
569  new (std::nothrow)
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);
573  }
574  template <class FUNC, class T1, class T2, class T3>
575  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2, const T3& arg3, move_tag) {
577  new (std::nothrow)
578  ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3, typename ArgType<T3>::type()));
579  if (!ptr) return ENOMEM;
580  return this->StartRaw(ptr);
581  }
582  template <class FUNC, class T1, class T2, class T3, class T4>
583  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
590  }
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,
593  move_tag) {
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);
599  }
600  template <class FUNC, class T1, class T2, class T3, class T4, class T5>
601  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
608  }
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,
611  const T5& arg5, move_tag) {
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);
617  }
618  template <class FUNC>
619  errno_t Start(const ThreadSettings& settings, const FUNC& f) {
620  UniquePtr<ThreadFuncX0<FUNC> > ptr(new (std::nothrow) ThreadFuncX0<FUNC>(f));
621  if (!ptr) return ENOMEM;
622  return this->StartRaw(settings, ptr);
623  }
624  template <class FUNC>
625  errno_t Start(const FUNC& f) {
626  UniquePtr<ThreadFuncX0<FUNC> > ptr(new (std::nothrow) ThreadFuncX0<FUNC>(f));
627  if (!ptr) return ENOMEM;
628  return this->StartRaw(ptr);
629  }
630  template <class FUNC, class T1>
631  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1) {
632  UniquePtr<ThreadFuncX1<FUNC, T1> > ptr(new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1));
633  if (!ptr) return ENOMEM;
634  return this->StartRaw(settings, ptr);
635  }
636  template <class FUNC, class T1>
637  errno_t Start(const FUNC& f, const T1& arg1) {
638  UniquePtr<ThreadFuncX1<FUNC, T1> > ptr(new (std::nothrow) ThreadFuncX1<FUNC, T1>(f, arg1));
639  if (!ptr) return ENOMEM;
640  return this->StartRaw(ptr);
641  }
642  template <class FUNC, class T1, class T2>
643  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2) {
644  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow)
645  ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
646  if (!ptr) return ENOMEM;
647  return this->StartRaw(settings, ptr);
648  }
649  template <class FUNC, class T1, class T2>
650  errno_t Start(const FUNC& f, const T1& arg1, const T2& arg2) {
651  UniquePtr<ThreadFuncX2<FUNC, T1, T2> > ptr(new (std::nothrow)
652  ThreadFuncX2<FUNC, T1, T2>(f, arg1, arg2));
653  if (!ptr) return ENOMEM;
654  return this->StartRaw(ptr);
655  }
656  template <class FUNC, class T1, class T2, class T3>
657  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
658  const T3& arg3) {
660  new (std::nothrow) ThreadFuncX3<FUNC, T1, T2, T3>(f, arg1, arg2, arg3));
661  if (!ptr) return ENOMEM;
662  return this->StartRaw(settings, ptr);
663  }
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);
670  }
671  template <class FUNC, class T1, class T2, class T3, class T4>
672  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
678  }
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);
685  }
686  template <class FUNC, class T1, class T2, class T3, class T4, class T5>
687  errno_t Start(const ThreadSettings& settings, const FUNC& f, const T1& arg1, const T2& arg2,
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);
693  }
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,
696  const T5& arg5) {
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);
701  }
702 
703  private:
704  nlib_thread thread_id_;
706 };
707 
708 #ifdef _MSC_VER
709 #pragma warning(pop)
710 #endif
711 
712 template <>
713 struct Thread::Args0<void()> {
714  template <class X>
715  Args0(void (*func_)(), X)
716  : func(func_) {}
717  Args0(void (*func_)()) : func(func_) {}
718  void (*func)();
719 
720  private:
722 };
723 
724 template <class T1>
725 struct Thread::Args0<void(T1)> {
726  template <class X>
727  Args0(void (*func_)(T1), X)
728  : func(func_) {}
729  Args0(void (*func_)(T1)) : func(func_) {}
730  void (*func)(T1);
731 
732  private:
734 };
735 
736 template <class T1, class T2>
737 struct Thread::Args0<void(T1, T2)> {
738  template <class X>
739  Args0(void (*func_)(T1, T2), X)
740  : func(func_) {}
741  Args0(void (*func_)(T1, T2)) : func(func_) {}
742  void (*func)(T1, T2);
743 
744  private:
746 };
747 
748 template <class T1, class T2, class T3>
749 struct Thread::Args0<void(T1, T2, T3)> {
750  template <class X>
751  Args0(void (*func_)(T1, T2, T3), X)
752  : func(func_) {}
753  Args0(void (*func_)(T1, T2, T3)) : func(func_) {}
754  void (*func)(T1, T2, T3);
755 
756  private:
758 };
759 
760 template <class T1, class T2, class T3, class T4>
761 struct Thread::Args0<void(T1, T2, T3, T4)> {
762  template <class X>
763  Args0(void (*func_)(T1, T2, T3, T4), X)
764  : func(func_) {}
765  Args0(void (*func_)(T1, T2, T3, T4)) : func(func_) {}
766  void (*func)(T1, T2, T3, T4);
767 
768  private:
770 };
771 
772 template <class T1, class T2, class T3, class T4, class T5>
773 struct Thread::Args0<void(T1, T2, T3, T4, T5)> {
774  template <class X>
775  Args0(void (*func_)(T1, T2, T3, T4, T5), X)
776  : func(func_) {}
777  Args0(void (*func_)(T1, T2, T3, T4, T5)) : func(func_) {}
778  void (*func)(T1, T2, T3, T4, T5);
779 
780  private:
782 };
783 
785  unsigned int numCpu;
786  (void)nlib_thread_getconcurrency(&numCpu);
787  return numCpu;
788 }
789 
790 namespace this_thread {
792 inline errno_t Sleep(const TimeSpan& span) NLIB_NOEXCEPT {
794 }
795 inline errno_t SleepMilliSeconds(unsigned int millisec) NLIB_NOEXCEPT {
796  return Sleep(TimeSpan(0, 0, millisec));
797 }
799  nlib_thread_id id;
800  errno_t e = nlib_thread_getid(&id);
801  return e == 0 ? id : -1;
802 }
803 inline errno_t GetCpu(int* cpuid) NLIB_NOEXCEPT { return nlib_thread_getcpu(cpuid); }
804 inline errno_t SetName(const char* literal_string) NLIB_NOEXCEPT {
805  nlib_thread th;
806  errno_t e = nlib_thread_self(&th);
807  if (e != 0) return e;
808  return nlib_thread_setname(th, literal_string);
809 }
812 
813 } // namespace this_thread
814 
815 } // namespace threading
816 NLIB_NAMESPACE_END
817 
818 NLIB_DEFINE_STD_SWAP(::nlib_ns::threading::Thread)
819 
820 #endif // INCLUDE_NN_NLIB_THREADING_THREAD_H_
errno_t GetCpu(int *cpuid) noexcept
呼び出したスレッドが実行されているCPUを取得します。
Definition: Thread.h:803
errno_t YieldThread() noexcept
他のスレッドに制御を譲ります。
Definition: Thread.h:791
errno_t Start(const ThreadSettings &settings, const FUNC &f, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:523
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
引数を2つもつ関数を別スレッドで実行開始します。 引数を2つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:559
nlib_thread GetNativeHandle() const noexcept
実装依存のスレッド識別子を取得します。
Definition: Thread.h:234
static errno_t Sleep(const TimeSpan &span) noexcept
span の期間だけスリープします。
Definition: Thread.h:247
int nlib_thread_equal(nlib_thread th1, nlib_thread th2)
2つのスレッドが同一スレッドを指すかどうかチェックします。
constexpr Thread() noexcept
デフォルトコンストラクタです。
Definition: Thread.h:202
int GetStackSize() const noexcept
設定されているスタックサイズを返します。
Definition: Thread.h:54
errno_t Sleep(const TimeSpan &span) noexcept
スリープします。
Definition: Thread.h:792
constexpr ThreadArg(Func func_, T1 arg1_, T2 arg2_, T3 arg3_, T4 arg4_, T5 arg5_)
コンストラクタで構造体のフィールドを初期化します。
Definition: Thread.h:110
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:657
bool operator!=(const Thread &rhs) noexcept
nlib_thread_equal()を用いてスレッドを比較します。
Definition: Thread.h:242
constexpr ThreadArg()
デフォルトコンストラクタです。
Definition: Thread.h:109
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:145
T1 arg1
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:115
errno_t Detach() noexcept
スレッドをデタッチします。
Definition: Thread.h:219
Func func
別スレッドで実行する関数へのポインタです。
Definition: Thread.h:114
bool GetDetachState() const noexcept
デタッチ状態でスレッドを起動する設定かどうかを返します。
Definition: Thread.h:35
errno_t nlib_thread_setaffinity(nlib_thread thread, uint32_t affinity)
指定されたスレッドのプロセッサアフィニティマスクを設定します。
errno_t GetPriority(int32_t *priority) noexcept
スレッドの優先度を取得します。
Definition: Thread.h:225
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:610
~Thread() noexcept
デストラクタです。スレッドがJoinされていない場合はJoinします。
Definition: Thread.h:203
struct nlib_thread_attr_ nlib_thread_attr
新しく作られるスレッドに適用されるスレッド属性
Definition: Platform.h:1448
errno_t ChangePriority(int32_t priority) noexcept
スレッドの優先度を設定します。
Definition: Thread.h:228
T5 arg5
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:119
void SetDetachState(bool detached) noexcept
デタッチした状態でスレッドを起動するかどうかを設定します。
Definition: Thread.h:26
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:650
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
引数を3つもつ関数を別スレッドで実行開始します。引数を3つ持つこと以外は、引数が1つのバージョンと同じで...
Definition: Thread.h:665
UniquePtrはポインタの所有権を保持し、UniquePtrがスコープから出るときにデストラクタでポインタをDELで指...
Definition: UniquePtr.h:96
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:695
std::unique_ptrに相当するクラスが定義されています。
スレッドの生成・開始を行うためのクラスです。
Definition: Thread.h:199
errno_t nlib_thread_getid(nlib_thread_id *id)
実行中のスレッドに対応する一意の整数値を格納する。
Threadクラスでスレッドを実行するために利用できる構造体です。
Definition: Thread.h:105
void swap(Thread &rhs) noexcept
オブジェクトの内容をスワップします。
Definition: Thread.h:235
errno_t Start(const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
引数を3つもつ関数を別スレッドで実行開始します。 引数を3つ持つこと以外は、引数が1つのバージョンと同じ...
Definition: Thread.h:575
errno_t nlib_thread_attr_setint(nlib_thread_attr *attr, int key, int value)
スレッドの属性オブジェクトのキーに対応する整数を設定する。
int GetPriority() const noexcept
スレッドの優先度を取得します。
Definition: Thread.h:73
bool IsJoinable() const noexcept
join可能かどうか調べます。
Definition: Thread.h:224
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:61
errno_t nlib_thread_getconcurrency(unsigned int *num_cpu)
ハードウェアスレッドの数を取得します。
errno_t SetStackSize(int size) noexcept
スタックサイズを設定します。
Definition: Thread.h:46
errno_t Start(const FUNC &f, const T1 &arg1, move_tag)
引数を1つもつ関数を別スレッドで実行開始します。
Definition: Thread.h:544
errno_t StartRaw(UniquePtr< ThArg > &ptr) noexcept
別スレッドの実行を開始します。
Definition: Thread.h:270
errno_t SetName(const char *literal_string) noexcept
スレッドに名前をつけます。
Definition: Thread.h:804
errno_t nlib_yield(void)
スレッドの実行権を手放す。
nlib_thread_attrをラップするクラスです。必要に応じて自動的にnlib_thread_attr_init()とnlib_thread_attr...
Definition: Thread.h:16
errno_t StartRaw(const ThreadSettings &settings, UniquePtr< ThArg > &ptr) noexcept
別スレッドの実行を開始します。
Definition: Thread.h:261
static errno_t YieldThread() noexcept
nlib_yield()を呼び出します。
Definition: Thread.h:246
errno_t Join() noexcept
スレッドの実行完了を待ちます。
Definition: Thread.h:209
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:566
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:231
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:631
bool operator==(const Thread &rhs) noexcept
nlib_thread_equal()を用いてスレッドを比較します。
Definition: Thread.h:239
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:680
errno_t Start(const FUNC &f, const T1 &arg1)
引数を1つもつ関数を別スレッドで実行開始します。
Definition: Thread.h:637
#define NLIB_THREAD_INVALID
無効なスレッドを指し示す値
Definition: Platform.h:1432
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:619
errno_t SetPriority(int priority) noexcept
スレッドの優先度を設定します。優先度の値はプラットフォーム依存です。
Definition: Thread.h:65
時刻や時間を扱うためのクラスが定義されています。
nlib_thread_id GetId() noexcept
カレントスレッドのIDを取得します。
Definition: Thread.h:798
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:86
errno_t nlib_thread_attr_init(nlib_thread_attr *attr)
スレッド属性オブジェクトを初期化して、デフォルトに設定する。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:80
開発環境別の設定が書かれるファイルです。
errno_t nlib_sleep(nlib_duration t)
t の間スリープする。
errno_t Start(const FUNC &f)
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:625
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:672
errno_t nlib_thread_setpriority(nlib_thread thread, int priority)
スレッドの実行優先度を設定します。数値の意味は実装依存です。
int nlib_thread_id
スレッド毎にユニークな整数値
Definition: Platform.h:1458
size_t GetHardwareConcurrency() noexcept
ハードウェアスレッドの数を返します。
Definition: Thread.h:784
errno_t SetAffinity(uint32_t affinity) noexcept
スレッドのプロセッサアフィニティマスクを設定します。
Definition: Thread.h:231
errno_t nlib_thread_join(nlib_thread thread)
スレッドの終了を待ちます。
ThreadArg< T1, T2, T3, T4, T5 > ThisType
この構造体の型へのtypedefです。
Definition: Thread.h:106
errno_t StartRaw(const ThreadSettings &settings, ThreadArg<>::Func func) noexcept
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:252
UniquePtr< ThisType > ArgType
スレッド関数の引数型のtypedefです。
Definition: Thread.h:107
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:583
errno_t nlib_thread_detach(nlib_thread thread)
実行中のスレッドをデタッチ状態にします。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:211
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:687
errno_t nlib_thread_getcpu(int *result)
呼び出したスレッドが実行されているCPUを取得します。
errno_t nlib_thread_getpriority(nlib_thread thread, int *priority)
スレッドの現在の実行優先度を取得します。数値の意味は実装依存です。
時間を表すクラスです。
Definition: DateTime.h:93
errno_t Start(const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
スレッドの起動オプションを指定できること以外はそうでないバージョンと同じです。
Definition: Thread.h:551
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:601
errno_t SleepMilliSeconds(unsigned int millisec) noexcept
スリープします。
Definition: Thread.h:795
errno_t StartRaw(ThreadArg<>::Func func) noexcept
引数を持たない関数を別スレッドで実行開始します。
Definition: Thread.h:256
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:537
T2 arg2
別スレッドで実行する関数に渡されるオブジェクトです。
Definition: Thread.h:116
pthread_t nlib_thread
スレッドを指し示す識別子
errno_t Start(const FUNC &f, move_tag)
引数を持たない関数を別スレッドで実行開始します。 引数を持たないこと以外は、引数が1つのバージョンと同...
Definition: Thread.h:530
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:592
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:643
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24