wibble 0.1.28
thread.h
Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * OO encapsulation of Posix threads
00003  *
00004  * Copyright (C) 2003--2008  Enrico Zini <enrico@debian.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00019  */
00020 
00021 #ifndef WIBBLE_SYS_THREAD_H
00022 #define WIBBLE_SYS_THREAD_H
00023 
00024 #include <wibble/sys/macros.h>
00025 #include <wibble/exception.h>
00026 #ifdef POSIX
00027 #include <pthread.h>
00028 #endif
00029 
00030 #ifdef _WIN32
00031 #include <windows.h>
00032 #include <process.h>
00033 #endif
00034 #include <signal.h>
00035 
00036 namespace wibble {
00037 namespace sys {
00038 
00039 static inline void sleep( int secs ) {
00040 #ifdef _WIN32
00041     Sleep( secs * 1000 );
00042 #else
00043     ::sleep( secs );
00044 #endif
00045 }
00046 
00047 static inline void usleep( int usecs ) {
00048 #ifdef _WIN32
00049     Sleep( usecs / 1000 );
00050 #else
00051     ::usleep( usecs );
00052 #endif
00053 }
00054 
00092 class Thread
00093 {
00094 protected:
00095 #ifdef POSIX
00096     pthread_t thread;
00097 #endif
00098 
00099 #ifdef _WIN32
00100   unsigned int thread;
00101   HANDLE hThread;
00102 #endif
00103 
00108     virtual const char* threadTag() { return "generic"; }
00109     
00114     virtual void* main() = 0;
00115 
00117 #ifdef POSIX
00118     static void* Starter(void* parm);
00119 #endif
00120 
00121 #ifdef _WIN32
00122   static unsigned __stdcall Starter(void* parm);
00123 #endif
00124 
00125     void testcancel();
00126 
00127 public:
00128     virtual ~Thread() {}
00129 
00131     void start();
00132 
00134     void startDetached();
00135 
00137     void* join();
00138 
00140     void detach();
00141 
00143     void cancel();
00144 
00146     void kill(int signal);
00147 };
00148 
00149 }
00150 }
00151 
00152 // vim:set ts=4 sw=4:
00153 #endif