wibble  1.1
thread.h
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * OO encapsulation of Posix threads
3  *
4  * Copyright (C) 2003--2013 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 #ifdef POSIX
27 #include <pthread.h>
28 #include <unistd.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 
41 void sleep( int secs );
42 
44 void usleep( int usecs );
45 
83 class Thread
84 {
85 protected:
86 #ifdef POSIX
87  pthread_t thread;
88 #endif
89 
90 #ifdef _WIN32
91  void *_result;
92  unsigned int thread;
93  HANDLE hThread;
94 #endif
95 
100  virtual const char* threadTag() { return "generic"; }
101 
106  virtual void* main() = 0;
107 
109 #ifdef POSIX
110  static void* Starter(void* parm);
111 #endif
112 
113 #ifdef _WIN32
114  static unsigned __stdcall Starter(void* parm);
115 #endif
116 
117  void testcancel();
118 
119 public:
120  virtual ~Thread() {}
121 
123  void start();
124 
126  void startDetached();
127 
129  void* join();
130 
132  void detach();
133 
135  void cancel();
136 
138  void kill(int signal);
139 };
140 
141 }
142 }
143 
144 // vim:set ts=4 sw=4:
145 #endif
void detach()
Put the thread in the detached state.
Definition: thread.cpp:124
void start()
Start the thread.
Definition: thread.cpp:70
void kill(int signal)
Sent a signal to the thread.
Definition: thread.cpp:142
virtual void * main()=0
Main thread function, executed in the new thread after creation.
virtual const char * threadTag()
Short tag describing this thread, used in error messages and identification.
Definition: thread.h:100
void sleep(int secs)
Portable version of sleep.
Definition: thread.cpp:31
Encapsulates a thread.
Definition: thread.h:83
void cancel()
Send a cancellation request to the thread.
Definition: thread.cpp:133
void * join()
Join the thread.
Definition: thread.cpp:100
Definition: amorph.h:17
void startDetached()
Start the thread in the detached state.
Definition: thread.cpp:87
virtual ~Thread()
Definition: thread.h:120
void usleep(int usecs)
Portable version of usleep.
Definition: thread.cpp:39
void testcancel()
Callback function used to start the thread.
Definition: thread.cpp:63