Ice 3.7 C++11 API Reference
Loading...
Searching...
No Matches
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
13namespace IceUtil
14{
15
16class Time;
17
19{
20public:
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
81private:
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
98class ICE_API Thread : public virtual IceUtil::Shared
99{
100public:
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
132protected:
133 const std::string _name;
137
138#if defined(_WIN32)
139 HANDLE _handle;
140 DWORD _id;
141#else
142 pthread_t _thread;
143#endif
144
145private:
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
#define ICE_API
Definition Config.h:197
Definition Handle.h:143
Definition Mutex.h:33
Definition Shared.h:78
Definition Thread.h:19
static void sleep(const Time &)
bool operator==(const ThreadControl &) const
pthread_t ID
Definition Thread.h:74
bool operator!=(const ThreadControl &) const
const std::string & name() const
bool operator==(const Thread &) const
ThreadControl start(size_t=0)
ThreadControl start(size_t, int priority)
bool _started
Definition Thread.h:135
ThreadControl getThreadControl() const
virtual void run()=0
bool _running
Definition Thread.h:136
virtual ~Thread()
pthread_t _thread
Definition Thread.h:142
bool isAlive() const
Mutex _stateMutex
Definition Thread.h:134
const std::string _name
Definition Thread.h:133
bool operator<(const Thread &) const
Thread(const std::string &)
Definition Time.h:18
Definition Optional.h:1095
Handle< Thread > ThreadPtr
Definition Thread.h:156