X-Boost  2.3.8
thread.h
Go to the documentation of this file.
1 /******************************************************************************
2 * SPRINT::thread
3 *
4 * Copyright (C) 2003-2011 Paolo Medici <www.pmx.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *******************************************************************************/
21 
27 #ifndef _THREAD_H
28 #define _THREAD_H
29 
30 #include <unistd.h>
31 #include "function.h"
32 
33 #ifdef WIN32
34 
35 namespace sprint {
36 
43 class thread {
44  HANDLE hThread;
45  DWORD dwThreadId;
46 
47  private:
49  thread(const thread & src) { }
50 
51  public:
52 
54  thread() : hThread(INVALID_HANDLE_VALUE), dwThreadId(0) {}
55 
56  ~thread()
57  {
58  if(hThread != INVALID_HANDLE_VALUE)
59  ::CloseHandle(hThread);
60  }
61 
62 
68  inline bool create_thread(const sprint::thread_function & p)
69  {
70  hThread = ::CreateThread(NULL,
71  0,
72  p.proc,
73  p.param,
74  0,
75  &dwThreadId);
76  return hThread != INVALID_HANDLE_VALUE;
77  }
78 
79 
82  inline bool get_exit_code(LPDWORD lpdwExitCode)
83  {
84  return ::GetExitCodeThread(hThread, lpdwExitCode)!=0;
85  }
86 
89  inline bool is_terminated() const
90  {
91  if(hThread==INVALID_HANDLE_VALUE) return true;
92  return ::WaitForSingleObject(hThread, 0) == WAIT_OBJECT_0 ;
93  }
94 
97  inline bool is_running() const
98  {
99  if(hThread==INVALID_HANDLE_VALUE) return false;
100  return ::WaitForSingleObject(hThread, 0) != WAIT_OBJECT_0 ;
101  }
102 
105  inline bool kill()
106  {
107  return ::TerminateThread(hThread, 0)!=0;
108  }
109 
111  bool join(unsigned int timeout = INFINITE)
112  {
113  return ::WaitForSingleObject(hThread, timeout) == WAIT_OBJECT_0 ;
114  }
115 
116  static int hardware_concurrency()
117  {
118  SYSTEM_INFO info={{0}};
119  ::GetSystemInfo(&info);
120  return info.dwNumberOfProcessors;
121  }
122 
123 };
124 
138 class runnable: public thread {
139 
140  // jump method
141  static DWORD __stdcall CALLBACK _thread_proc(void *p)
142  {
143  return (DWORD) (reinterpret_cast<runnable*>(p))->the_thread();
144  }
145 
146  public:
147 
149  virtual void * the_thread() = 0;
150 
151  virtual ~runnable() { }
152 
154  void run()
155  {
156  thread::create_thread(sprint::thread_function(_thread_proc, reinterpret_cast<void*>(this)) );
157  }
158 
159 };
160 
161 
162 } // sprint
163 
164 #else
165 
166 #include <pthread.h>
167 
168 namespace sprint {
169 
170 class thread {
171  pthread_t m_thread;
172  private:
174  thread(const thread & src) { }
175  public:
176 
177  public:
178 
180  thread() {}
181 
182  ~thread()
183  {
184  }
185 
187  inline bool create_thread(const sprint::thread_function & p)
188  {
189  int ret = pthread_create(&m_thread, 0,
190  p.proc,
191  p.param);
192  return ret==0;
193  }
194 
196  inline bool kill() const
197  {
198  // TODO
199  return false;
200  }
201 
203  bool join() const
204  {
205  return pthread_join(m_thread, 0);
206  }
207 
208  static int hardware_concurrency()
209  {
210  int const count=sysconf(_SC_NPROCESSORS_ONLN);
211  return (count>0)?count:0;
212  }
213 };
214 
215 class runnable: public thread {
216 
217  static void * _thread_proc(void *p)
218  {
219  return (void *) (reinterpret_cast<runnable*>(p))->the_thread();
220  }
221 
222  public:
223  virtual void * the_thread() = 0;
224 
225  virtual ~runnable() { }
226 
228  void run()
229  {
230  thread::create_thread(sprint::thread_function(_thread_proc, reinterpret_cast<void*>(this)) );
231  }
232 };
233 
234 }
235 
236 #endif
237 
238 #endif
pthread thread creation params
Definition: function.h:95
Definition: thread.h:170
bool join() const
wait for thread terminataion
Definition: thread.h:203
Definition: thread.h:215
bool kill() const
force termination of thread
Definition: thread.h:196
bool create_thread(const sprint::thread_function &p)
run per C thread using a sprint::thread::function
Definition: thread.h:187
thread()
Ctor.
Definition: thread.h:180
void run()
run the thread method
Definition: thread.h:228