Ice 3.7 C++11 API Reference
Thread.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_THREAD_H
6 #define ICE_UTIL_THREAD_H
7 
8 #include <IceUtil/Config.h>
9 #include <IceUtil/Shared.h>
10 #include <IceUtil/Handle.h>
11 #include <IceUtil/Mutex.h>
12 
13 namespace IceUtil
14 {
15 
16 class Time;
17 
19 {
20 public:
21 
22  //
23  // Constructs a ThreadControl representing the current thread.
24  // join and detach cannot be called on such ThreadControl object.
25  //
27 
28 #if defined(_WIN32)
29  ThreadControl(HANDLE, DWORD);
30 #else
31  explicit ThreadControl(pthread_t);
32 #endif
33 
34  //
35  // Default copy destructor, assignment operator and destructor OK
36  //
37 
38  //
39  // == and != are meaningful only before the thread is joined/detached,
40  // or while the thread is still running.
41  //
42  bool operator==(const ThreadControl&) const;
43  bool operator!=(const ThreadControl&) const;
44 
45  //
46  // Wait until the controlled thread terminates. The call has POSIX
47  // semantics.
48  //
49  // At most one thread can wait for the termination of a given
50  // thread. Calling join on a thread on which another thread is
51  // already waiting for termination results in undefined behaviour,
52  // as does joining with a thread after having joined with it
53  // previously, or joining with a detached thread.
54  //
55  void join();
56 
57  //
58  // Detach a thread. Once a thread is detached, it cannot be
59  // detached again, nor can it be joined with. Every thread that
60  // was created using the IceUtil::Thread class must either be
61  // joined with or detached exactly once. Detaching a thread a
62  // second time, or detaching a thread that was previously joined
63  // with results in undefined behavior.
64  //
65  void detach();
66 
67  //
68  // id() returns the Thread ID on Windows and the underlying pthread_t
69  // on POSIX platforms.
70  //
71 #if defined(_WIN32)
72  typedef DWORD ID;
73 #else
74  typedef pthread_t ID;
75 #endif
76  ID id() const;
77 
78  static void sleep(const Time&);
79  static void yield();
80 
81 private:
82 
83 #if defined(_WIN32)
84  HANDLE _handle;
85  DWORD _id;
86 #else
87  pthread_t _thread;
88 
89  //
90  // Used to prevent joining/detaching a ThreadControl constructed
91  // with the default constructor. Only needed to enforce our
92  // portable join/detach behavior.
93  //
94  bool _detachable;
95 #endif
96 };
97 
98 class ICE_API Thread : public virtual IceUtil::Shared
99 {
100 public:
101 
103  Thread(const std::string&);
104  virtual ~Thread();
105 
106  virtual void run() = 0;
107 
108  ThreadControl start(size_t = 0);
109  ThreadControl start(size_t, int priority);
110 
112 
113  bool operator==(const Thread&) const;
114  bool operator<(const Thread&) const;
115 
116  //
117  // Check whether a thread is still alive.
118  //
119  bool isAlive() const;
120 
121  //
122  // This function is an implementation detail;
123  // do not call it.
124  //
125  void _done();
126 
127  //
128  // Get the thread name
129  //
130  const std::string& name() const;
131 
132 protected:
133  const std::string _name;
135  bool _started;
136  bool _running;
137 
138 #if defined(_WIN32)
139  HANDLE _handle;
140  DWORD _id;
141 #else
142  pthread_t _thread;
143 #endif
144 
145 private:
146 
147 #ifdef _WIN32
148 #else
149  ThreadControl start(size_t, bool, int);
150 #endif
151 
152  Thread(const Thread&); // Copying is forbidden
153  void operator=(const Thread&); // Assignment is forbidden
154 };
155 
157 
158 }
159 
160 #endif
IceUtil::ThreadControl::yield
static void yield()
IceUtil::Thread::Thread
Thread(const std::string &)
IceUtil::ThreadControl::join
void join()
IceUtil::Thread::operator==
bool operator==(const Thread &) const
IceUtil::ThreadControl::sleep
static void sleep(const Time &)
IceUtil::Thread::Thread
Thread()
IceUtil::ThreadControl::detach
void detach()
Handle.h
IceUtil::Thread::start
ThreadControl start(size_t, int priority)
IceUtil::Thread
Definition: Thread.h:99
IceUtil
Definition: Optional.h:1095
ICE_API
#define ICE_API
Definition: Config.h:197
IceUtil::Thread::_done
void _done()
IceUtil::Thread::_started
bool _started
Definition: Thread.h:135
IceUtil::ThreadControl
Definition: Thread.h:19
IceUtil::Thread::getThreadControl
ThreadControl getThreadControl() const
IceUtil::Thread::operator<
bool operator<(const Thread &) const
Shared.h
IceUtil::Time
Definition: Time.h:18
IceUtil::Thread::run
virtual void run()=0
IceUtil::ThreadControl::operator==
bool operator==(const ThreadControl &) const
IceUtil::Thread::_thread
pthread_t _thread
Definition: Thread.h:142
Config.h
IceUtil::Thread::~Thread
virtual ~Thread()
IceUtil::Mutex
Definition: Mutex.h:33
IceUtil::Thread::_running
bool _running
Definition: Thread.h:136
Mutex.h
IceUtil::ThreadControl::ID
pthread_t ID
Definition: Thread.h:74
IceUtil::Handle
Definition: Handle.h:143
IceUtil::ThreadControl::ThreadControl
ThreadControl()
IceUtil::Thread::start
ThreadControl start(size_t=0)
IceUtil::ThreadPtr
Handle< Thread > ThreadPtr
Definition: Thread.h:156
IceUtil::Thread::name
const std::string & name() const
IceUtil::ThreadControl::id
ID id() const
IceUtil::Thread::isAlive
bool isAlive() const
IceUtil::Shared
Definition: Shared.h:78
IceUtil::ThreadControl::ThreadControl
ThreadControl(pthread_t)
IceUtil::Thread::_stateMutex
Mutex _stateMutex
Definition: Thread.h:134
IceUtil::Thread::_name
const std::string _name
Definition: Thread.h:133
IceUtil::ThreadControl::operator!=
bool operator!=(const ThreadControl &) const