FRI-OS
thread_attr.hpp
Go to the documentation of this file.
1 
4 #ifndef __FRIOS_POSIX_THREADS_THREAD_ATTR_HPP__
5 #define __FRIOS_POSIX_THREADS_THREAD_ATTR_HPP__
6 
7 #include <frios/utils/unique.hpp>
8 #include <frios/posix/error.hpp>
9 
10 #include <pthread.h>
11 
12 namespace frios {
13 namespace posix {
14 
15 class thread;
16 
18 
23 {
24 private:
25  friend class thread;
26 
28 
29  thread_attr(const thread_attr&) = delete;
30 public:
32 
34  struct init_error : error
35  {
36  init_error(int error_code)
37  : error(error_code)
38  { }
39  };
40 
42 
46  {
47  int fail = pthread_attr_init(&_attr);
48  if(fail) throw init_error(fail);
49  }
50 
53  : _attr(std::move(tmp._attr))
54  { }
55 
58  {
59  if(_attr.valid())
60  pthread_attr_destroy(&_attr);
61  }
62 
64  enum class detach_state : int
65  {
67  detached = PTHREAD_CREATE_DETACHED,
69  joinable = PTHREAD_CREATE_JOINABLE
70  };
71 
74  {
75  int state;
76  pthread_attr_getdetachstate(&_attr, &state);
77  return detach_state(state);
78  }
79 
82  {
83  pthread_attr_setdetachstate(&_attr, int(state));
84  }
85 };
86 
87 } // namespace posix
88 } // namespace frios
89 
90 #endif // include guard
Wrapper around POSIX thread attributes.
Definition: thread_attr.hpp:22
Exception class for thread attribute initialization errors.
Definition: thread_attr.hpp:34
the thread should be started as detached
thread_attr(void)
Default construction of the thread attributes.
Definition: thread_attr.hpp:45
void set_detach_state(detach_state state)
Sets the detach state of the thread.
Definition: thread_attr.hpp:81
Base class for all POSIX exceptions.
Definition: error.hpp:14
detach_state
Thread detach state enumeration.
Definition: thread_attr.hpp:64
Utility for ensuring uniqueness of a variable.
the thread should be started as joinable
Declaration of exception classes for POSIX errors.
thread_attr(thread_attr &&tmp)
Thread attributes are moveable.
Definition: thread_attr.hpp:52
error(int error_code)
Construction from POSIX error code.
Definition: error.hpp:26
Wrapper around a POSIX thread.
Definition: thread.hpp:25
~thread_attr(void)
Releases any resources associated with the thread attributes.
Definition: thread_attr.hpp:57
detach_state get_detach_state(void) const
Gets the detach state set in the attributes.
Definition: thread_attr.hpp:73