📄 athread.hh
字号:
/* File: athread.hh By: Alex Theo de Jong Created: March 1996 Version: 0.1 Description: Alex' Threads (athread) and synchronization objects, based on pthreads, Solaris threads, and Irix sprocs. This library is a thin shell around the previous MT solutions to use multi-processing with a shared memory space. The interface is similar to the POSIX interface. Feel free to change the interface names to anything you like :-) Compiler variable (select one): SOLARIS SOLARIS_PTHREAD IRIX IRIX_PTHREAD LINUX Synchronization Objects: class MutexLock { int lock(); int unlock(); } class Semaphore { int P(); int V(); } class Condition { int wait(MutexLock* lock); int timedwait(MutexLock* lock); int signal(); int broadcast(); } Threads: int athr_create(); int athr_kill(); int athr_yield(); int athr_equal(athr_t t1, athr_t t2); int athr_join(); int athr_ Notes: - No considerable time has been spent on performance. This is something that should be done; - Pthreads are not able to work with X11 and the GNU C++ stream classes. This seems to be caused by the pthread library; */#ifndef __athread_hh#define __athread_hhclass Errors { static int errcount; // Number of classes using Error for debugging protected: int errnumber; // Last error number char errstr[50]; // Last error string Errors(int e=0) : errnumber(e) { errstr[0]='\0'; errcount++; } public: int geterrno() const { return errnumber; } const char* geterrstr() const { return (char*) errstr; }};#if (defined( SOLARIS_PTHREAD) || defined(LINUX)) #ifndef _REENTRANT#define _REENTRANT#endif#include <pthread.h>typedef pthread_t athr_t;class MutexLock : public Errors { friend class Condition; pthread_mutex_t mlock; public: MutexLock(){ errnumber=pthread_mutex_init(&mlock, 0); } ~MutexLock(){ errnumber=pthread_mutex_destroy(&mlock); } int lock(){ return errnumber=pthread_mutex_lock(&mlock); }// int lock(){ return errnumber=pthread_mutex_unlock(&mlock); } int unlock(){ return errnumber=pthread_mutex_unlock(&mlock); }};class Semaphore : public Errors { pthread_cond_t condition; pthread_mutex_t mlock; int count; public: Semaphore(int value=0) : count(value) { errnumber=pthread_cond_init(&condition, 0); errnumber=pthread_mutex_init(&mlock, 0); } ~Semaphore(){ errnumber=pthread_cond_destroy(&condition); errnumber=pthread_mutex_destroy(&mlock); } int P(){ errnumber=pthread_mutex_unlock(&mlock); count--; while (count<0) errnumber=pthread_cond_wait(&condition, &mlock); errnumber=pthread_mutex_unlock(&mlock); return errnumber; } int V(){ errnumber=pthread_mutex_unlock(&mlock); count++; errnumber=pthread_cond_signal(&condition); errnumber=pthread_mutex_unlock(&mlock); return errnumber; }};class Condition : public Errors { pthread_cond_t condition; public: Condition(){ errnumber=pthread_cond_init(&condition, 0); } ~Condition(){ errnumber=pthread_cond_destroy(&condition); } int signal(){ return errnumber=pthread_cond_signal(&condition); } int broadcast(){ return errnumber=pthread_cond_broadcast(&condition); } int wait(MutexLock* l){ return errnumber=pthread_cond_wait(&condition, &l->mlock); } int timedwait(MutexLock* l, const timespec* time){ return errnumber=pthread_cond_timedwait(&condition, &l->mlock, time); }};inline int athr_create(void* (*function)(void*), void* arg, athr_t* id){ return pthread_create(id, 0, function, arg);}inline int athr_join(athr_t id){ return pthread_join(id, 0); }inline void athr_yield(){ sched_yield(); }//inline void athr_yield(){ pthread_yield(); }inline void athr_exit(void* code){ pthread_exit(code); }inline int athr_suspend(athr_t& id){ return -1; } // return pthread_suspend(id); }inline int athr_continue(athr_t& id){ return -1; } // return pthread_continue(id); }inline athr_t athr_self(){ return pthread_self(); }inline int athr_kill(athr_t thread, int signal){ return pthread_kill(thread, signal); }inline int athr_equal(athr_t thread1, athr_t thread2){ return pthread_equal(thread1, thread2); }inline int athr_detach(athr_t thread){ return pthread_detach(thread); }inline int athr_setschedparam(athr_t t, int p, sched_param* param){ return pthread_setschedparam(t, p, param); }inline int athr_getschedparam(athr_t t, int* p, sched_param* param){ return pthread_getschedparam(t, p, param); }#endif // SOLARIS_PTHREAD#ifdef SOLARIS#ifndef _REENTRANT#define _REENTRANT#endif#include <thread.h>typedef thread_t athr_t;struct sched_param { // copied from pthread in order to stay compatible int prio; void* no_data; sched_param() : prio(0) { no_data=0; }};class MutexLock : public Errors { friend class Condition; mutex_t mlock; public: MutexLock(){ errnumber=mutex_init(&mlock, USYNC_THREAD, 0); } ~MutexLock(){ errnumber=mutex_destroy(&mlock); } int lock(){ return errnumber=mutex_unlock(&mlock); } int unlock(){ return errnumber=mutex_unlock(&mlock); }};class Semaphore : public Errors { sema_t sema; public: Semaphore(int value=0){ errnumber=sema_init(&sema, value, USYNC_THREAD, 0); } ~Semaphore(){ errnumber=sema_destroy(&sema); } int P(){ return errnumber=sema_wait(&sema); } int V(){ return errnumber=sema_post(&sema); }};class Condition : public Errors { cond_t condition; public: Condition(){ errnumber=cond_init(&condition, USYNC_THREAD, 0); } ~Condition(){ errnumber=cond_destroy(&condition); } int signal(){ return errnumber=cond_signal(&condition); } int broadcast(){ return errnumber=cond_broadcast(&condition); } int wait(MutexLock* l){ return errnumber=cond_wait(&condition, &l->mlock); } int timedwait(MutexLock* l, const timespec* time){ return errnumber=cond_timedwait(&condition, &l->mlock, (timespec*) time); }};inline int athr_create(void* (*function)(void*), void* arg, athr_t* id){ return thr_create(0, 0, function, arg, 0, id);}inline int athr_join(athr_t id){ return thr_join(id, 0, 0); }inline void athr_yield(){ thr_yield(); }inline void athr_exit(void* code){ thr_exit(code); }inline int athr_suspend(athr_t& id){ return thr_suspend(id); }inline int athr_continue(athr_t& id){ return thr_continue(id); }inline athr_t athr_self(){ return thr_self(); }inline int athr_kill(athr_t thread, int signal){ return thr_kill(thread, signal); }inline int athr_equal(athr_t thread1, athr_t thread2){ return (thread1==thread2); }inline int athr_detach(athr_t){ return -1; } // thr_detach(thread); }inline int athr_setschedparam(athr_t t, int policy, sched_param* arg){ return (policy!=0) ? -1 : thr_setprio(t, arg->prio); }inline int athr_getschedparam(athr_t t, int* policy, sched_param* arg){ *policy=0; return thr_getprio(t, &arg->prio); }#endif // SOLARIS#ifdef IRIX_PTHREAD#ifndef _SGI_MP_SOURCE#define _SGI_MP_SOURCE#endif#ifndef _REENTRANT#define _REENTRANT#endif#include <pthread.h>typedef pthread_t athr_t;class MutexLock : public Errors { friend class Condition; pthread_mutex_t mlock; public: MutexLock(){ errnumber=pthread_mutex_init(&mlock, 0); } ~MutexLock(){ errnumber=pthread_mutex_destroy(&mlock); } int lock(){ return errnumber=pthread_mutex_lock(&mlock); } int unlock(){ return errnumber=pthread_mutex_unlock(&mlock); }};class Semaphore : public Errors { pthread_cond_t condition; pthread_mutex_t mlock; int count; public: Semaphore(int value=0) : count(value) { errnumber=pthread_cond_init(&condition, 0); errnumber=pthread_mutex_init(&mlock, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -