wibble  0.1.28
thread.h
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * OO encapsulation of Posix threads
3  *
4  * Copyright (C) 2003--2008 Enrico Zini <enrico@debian.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #ifndef WIBBLE_SYS_THREAD_H
22 #define WIBBLE_SYS_THREAD_H
23 
24 #include <wibble/sys/macros.h>
25 #include <wibble/exception.h>
26 #include <unistd.h>
27 #ifdef POSIX
28 #include <pthread.h>
29 #endif
30 
31 #ifdef _WIN32
32 #include <windows.h>
33 #include <process.h>
34 #endif
35 #include <signal.h>
36 
37 namespace wibble {
38 namespace sys {
39 
40 static inline void sleep( int secs ) {
41 #ifdef _WIN32
42  Sleep( secs * 1000 );
43 #else
44  ::sleep( secs );
45 #endif
46 }
47 
48 static inline void usleep( int usecs ) {
49 #ifdef _WIN32
50  Sleep( usecs / 1000 );
51 #else
52  ::usleep( usecs );
53 #endif
54 }
55 
93 class Thread
94 {
95 protected:
96 #ifdef POSIX
97  pthread_t thread;
98 #endif
99 
100 #ifdef _WIN32
101  unsigned int thread;
102  HANDLE hThread;
103 #endif
104 
109  virtual const char* threadTag() { return "generic"; }
110 
115  virtual void* main() = 0;
116 
118 #ifdef POSIX
119  static void* Starter(void* parm);
120 #endif
121 
122 #ifdef _WIN32
123  static unsigned __stdcall Starter(void* parm);
124 #endif
125 
126  void testcancel();
127 
128 public:
129  virtual ~Thread() {}
130 
132  void start();
133 
135  void startDetached();
136 
138  void* join();
139 
141  void detach();
142 
144  void cancel();
145 
147  void kill(int signal);
148 };
149 
150 }
151 }
152 
153 // vim:set ts=4 sw=4:
154 #endif