编程实例:
CThread.h
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <iostream>
class Runnable
{
public:
virtual ~Runnable() {}
virtual void* Run(void * arg) = 0;
};
class CThread
{
public:
CThread();
virtual ~CThread();
void Start(Runnable *r, void* arg);
void Stop();
int getpid() { return pid;}
int getthreadid() {return tid;}
private:
int CreateThread(pthread_t *threadid, void *pfunction, void *arg)
{
int ret = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
{
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if(ret < 0)
{
pthread_attr_destroy(&attr);
return ret;
}
}
pthread_create(threadid, &attr, (void*(*)(void*))pfunction, arg);
pthread_attr_destroy(&attr);
return ret;
}
static void* hook(void *arg) //global function
{
CThread *thread = (CThread*)arg;