• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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 #include <unistd.h>
00027 #ifdef POSIX
00028 #include <pthread.h>
00029 #endif
00030 
00031 #ifdef _WIN32
00032 #include <windows.h>
00033 #include <process.h>
00034 #endif
00035 #include <signal.h>
00036 
00037 namespace wibble {
00038 namespace sys {
00039 
00040 static inline void sleep( int secs ) {
00041 #ifdef _WIN32
00042     Sleep( secs * 1000 );
00043 #else
00044     ::sleep( secs );
00045 #endif
00046 }
00047 
00048 static inline void usleep( int usecs ) {
00049 #ifdef _WIN32
00050     Sleep( usecs / 1000 );
00051 #else
00052     ::usleep( usecs );
00053 #endif
00054 }
00055 
00093 class Thread
00094 {
00095 protected:
00096 #ifdef POSIX
00097     pthread_t thread;
00098 #endif
00099 
00100 #ifdef _WIN32
00101   unsigned int thread;
00102   HANDLE hThread;
00103 #endif
00104 
00109     virtual const char* threadTag() { return "generic"; }
00110     
00115     virtual void* main() = 0;
00116 
00118 #ifdef POSIX
00119     static void* Starter(void* parm);
00120 #endif
00121 
00122 #ifdef _WIN32
00123   static unsigned __stdcall Starter(void* parm);
00124 #endif
00125 
00126     void testcancel();
00127 
00128 public:
00129     virtual ~Thread() {}
00130 
00132     void start();
00133 
00135     void startDetached();
00136 
00138     void* join();
00139 
00141     void detach();
00142 
00144     void cancel();
00145 
00147     void kill(int signal);
00148 };
00149 
00150 }
00151 }
00152 
00153 // vim:set ts=4 sw=4:
00154 #endif

Generated on Sun Feb 17 2013 02:35:46 for wibble by  doxygen 1.7.1