c++封装多线程2

编程实例:

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;
		thread->pid = gettid();
		if(thread->runnable)
		{
			thread->runnable->Run(arg);
		}
		return (void*)NULL;
	}
private:
	static pid_t gettid(){return syscall(__NR_gettid);}
    pthread_t tid; //thread id
	int pid; //process id
	void *data;
	Runnable *runnable;
	CThread(const CThread&);
	CThread& operator=(const CThread& );
};

CThread::CThread()
{
	tid = 0;
	pid = 0;
}

CThread::~CThread()
{}

void CThread::Start(Runnable *r, void* arg)
{
	runnable = r;
	data = arg;
	if(CreateThread(&tid, (void*)CThread::hook, (void*)this) < 0)
	{
		std::cout << "create pthread err\n";
	}
	else
	{
		std::cout << "pthread start:"<<tid << "\n";
	}
}

void CThread::Stop()
{
	if(tid)
	{
		pthread_join(tid, NULL);
		tid = 0; pid = 0;
	}
}

class CDefultRunnable : public Runnable
{
public:
	CDefultRunnable(){ threadcount = 1;} 
	CDefultRunnable(int num):threadcount(num){}
	~CDefultRunnable(){}

	void SetThreadsCount(int num)
	{
		if(threads != NULL)
		{
			std::cout << "threads is running, can't set thread count\n";
			return;
		}
		threadcount = num;
	}
	void Start();
	void Stop();
	void Wait();
private:
	int threadcount; //
	CThread* threads;
};

void CDefultRunnable::Start()
{
	threads = new CThread[threadcount];
	if(threads)
	{
		for(int i=0; i<threadcount; ++i)
		{
			threads[i].Start(this, (void*)i);
		}
	}
}
void CDefultRunnable::Stop()
{	
}
void CDefultRunnable::Wait()
{
	if(threads != NULL)
	{
		for(int i=0; i< threadcount; ++i)
		{
			threads[i].Stop();
		}
	}
}

class T : public CDefultRunnable
{
public:
    T()
	{
		pthread_mutex_init(&mutex, NULL);
		d = 0;
	}
	T(int num):CDefultRunnable(num)
	{
		pthread_mutex_init(&mutex, NULL);
		d = 0;
	}
    virtual ~T(){d=0;}
	void* Run(void * arg);
private:
	T(const T& t);
	T& operator=(const T& t);
	int d;
	pthread_mutex_t mutex;
};

void* T::Run(void* arg)
{
	do
	{
		pthread_mutex_lock(&mutex);
		++d;		
		if(d > 1000) break;
		std::cout << d << "\n";
		pthread_mutex_unlock(&mutex);
	} while (d < 1000);
}

测试文件:

Test.cpp

#include "CThread.h"
#include <sys/time.h>
int main(int argc, char *argv[])
{
    // T t;
	// CThread *thread = new CThread();
	// thread->Start(&t, NULL);
	// std::cout << "OK\n";
	// thread->Stop();
	// sleep(1);
	// T t2;
	// thread->Start(&t2, NULL);
	// thread->Stop();
	// sleep(1);

	T *t = new T(5);
    //t->SetThreadsCount(5);
	t->Start();
	t->Stop();
	sleep(2);
    return 0;
}

编译:

g++ -o test Test.cpp CThread.h -lpthread

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值