00001
00002
00003 #ifndef _FRED_H_
00004 #define _FRED_H_
00005
00007
00008 #if defined(_WIN32)
00009
00010 #include <windows.h>
00011 #include <process.h>
00012
00013 #elif defined(__SVR4)
00014
00015 #include <thread.h>
00016 #include <pthread.h>
00017 #include <unistd.h>
00018
00019 #else
00020
00021 #include <pthread.h>
00022 #include <unistd.h>
00023
00024 #endif
00025
00026 namespace HL {
00027
00028 extern "C" typedef void * (*ThreadFunctionType) (void *);
00029
00030 class Fred {
00031 public:
00032
00033 Fred (void) {
00034 #if !defined(_WIN32)
00035 pthread_attr_init (&attr);
00036 #endif
00037 }
00038
00039 ~Fred (void) {
00040 #if !defined(_WIN32)
00041 pthread_attr_destroy (&attr);
00042 #endif
00043 }
00044
00045 void create (ThreadFunctionType function, void * arg) {
00046 #if defined(_WIN32)
00047 t = CreateThread (0, 0, (LPTHREAD_START_ROUTINE) *function, (LPVOID) arg, 0, 0);
00048 #else
00049 pthread_create (&t, &attr, function, arg);
00050 #endif
00051 }
00052
00053 void join (void) {
00054 #if defined(_WIN32)
00055 WaitForSingleObject (t, INFINITE);
00056 #else
00057 pthread_join (t, NULL);
00058 #endif
00059 }
00060
00061 static void yield (void) {
00062 #if defined(_WIN32)
00063 Sleep (0);
00064 #elif defined(__SVR4)
00065 thr_yield();
00066 #else
00067 sched_yield();
00068 #endif
00069 }
00070
00071
00072 static void setConcurrency (int n) {
00073 #if defined(_WIN32)
00074 #elif defined(__SVR4)
00075 thr_setconcurrency (n);
00076 #else
00077 pthread_setconcurrency (n);
00078 #endif
00079 }
00080
00081
00082 private:
00083 #if defined(_WIN32)
00084 typedef HANDLE FredType;
00085 #else
00086 typedef pthread_t FredType;
00087 pthread_attr_t attr;
00088 #endif
00089
00090 FredType t;
00091 };
00092
00093 }
00094
00095
00096 #endif