003.线程池

线程池实现,看类图和代码:

/*******************************************************************
** 文件名:	Thread.h
** 描  述:
** 应  用:
********************************************************************/
#pragma once
#include "Common.h"

//
/// 可运行对象
struct IRunnable
{
	virtual void run() = 0;
	virtual void release() = 0;
};

/// 简单线程封装
class T20_EXPORT Thread
{
protected:
	ulong	m_ThreadId;
	void*	m_ThreadHandle;

public:
	Thread();
	~Thread();

public:
	bool spawn(IRunnable* task, int priority = 0);
	void wait();
	void start();
	void pause();
	void terminate();
	static void sleep(unsigned long timeout);

protected:
	static unsigned int __stdcall _dispatch(void* arg);
};
/*******************************************************************
** 文件名:	Thread.cpp
** 描  述:
** 应  用:
********************************************************************/
#include "stdafx.h"
#include "Thread.h"
#include <process.h>

//
// 线程封装
Thread::Thread() : m_ThreadId(0), m_ThreadHandle(0)
{
}

Thread::~Thread()
{
	if ( m_ThreadHandle !=0 )
	{
		::TerminateThread(m_ThreadHandle,0);
		m_ThreadHandle = 0;
	}
}

bool Thread::spawn(IRunnable* task, int priority) 
{
	m_ThreadHandle = (HANDLE)::_beginthreadex(0, 0, &_dispatch, task, CREATE_SUSPENDED, (unsigned int*)&m_ThreadId);
	::SetThreadPriority(m_ThreadHandle, priority);
	::ResumeThread(m_ThreadHandle);
	return m_ThreadHandle != NULL;
}

void Thread::wait()
{
	if (m_ThreadHandle == NULL) { return; }
	::WaitForSingleObject(m_ThreadHandle, INFINITE);

	m_ThreadHandle = NULL;
}

void Thread::start()
{
	::ResumeThread(m_ThreadHandle);
}

void Thread::pause()
{
	::SuspendThread(m_ThreadHandle);
}

void Thread::terminate()
{
	::TerminateThread(m_ThreadHandle, 0);
	::CloseHandle(m_ThreadHandle);
	m_ThreadHandle = NULL;		
}

void Thread::sleep(unsigned long timeout)
{
	Sleep(timeout);
}

unsigned int __stdcall Thread::_dispatch(void* arg)
{
	IRunnable* task = reinterpret_cast<IRunnable*>(arg);
	assert(task);

	task->run();
	return 0;
}
/*******************************************************************
** 文件名:	ThreadPool.h
** 描  述:
** 应  用:
********************************************************************/
#pragma once

#include "Common.h"
#include "Thread.h"

//
// 线程池
class ThreadPool
{
protected:
	typedef std::vector<Thread*>	ThreadList;
	ThreadList	m_Threads;
	bool m_AutoRemove;	/// 当工作线程执行结束后,自动删除

public:
	ThreadPool::ThreadPool(void) {}
	ThreadPool::~ThreadPool(void) { clear(); }

	Thread * ThreadPool::add(IRunnable* task, int priority = 0)
	{
		Thread* thread = new Thread();
		if (!thread->spawn(task, priority))
		{
			return NULL;
		}

		m_Threads.push_back(thread);
		return thread;
	}

	void ThreadPool::start()
	{
		for (ThreadList::iterator it = m_Threads.begin(); it != m_Threads.end(); ++it)
		{
			(*it)->start();
		}
	}

	void ThreadPool::pause()
	{
		for (ThreadList::iterator it = m_Threads.begin(); it != m_Threads.end(); ++it)
		{
			(*it)->pause();
		}
	}

	void ThreadPool::wait()
	{
		for (ThreadList::iterator it = m_Threads.begin(); it != m_Threads.end(); ++it)
		{
			(*it)->wait();
		}
	}

	void ThreadPool::clear()
	{
		for (ThreadList::iterator it = m_Threads.begin(); it != m_Threads.end(); ++it)
		{
			delete *it;
		}

		m_Threads.clear();
	}

	void ThreadPool::remove(Thread * thread)
	{
		for (ThreadList::iterator it = m_Threads.begin(); it != m_Threads.end(); ++it)
		{
			if (thread == (*it))
			{
				delete *it;
				m_Threads.erase(it);
				return;
			}
		}
	}

	size_t getThreadCount() const	{ return m_Threads.size(); }
};
/*******************************************************************
** 文件名:	WorkerThreadPool.h 
** 描  述:
** 应  用:
********************************************************************/
#pragma once

#include "Common.h"
#include "singleton.h"
#include "ThreadPool.h"

/**
@name : 工作线程序组
@brief: 
*/
class WorkerThreadPool : public ThreadPool,public SingletonEx<WorkerThreadPool>
{
	typedef std::list<IRunnable*>	WorkerList;
	WorkerList m_workerList;
public:
	~WorkerThreadPool()
	{
		for (WorkerList::iterator it=m_workerList.begin(); it!=m_workerList.end(); ++it)
			(*it)->release();
		m_workerList.clear();
	}

	Thread * add(IRunnable* task, int priority = 0)
	{
		m_workerList.push_back(task);
		return ThreadPool::add(task, priority);
	}
};

测试:

#include <iostream>
#include "Common.h"
#include "Lock.h"
#include "WorkerThreadPool.h"
using namespace std;
Mutex gMu;
class Run1 : public IRunnable
{
public:
	void run()
	{
		ResGuard<Mutex> lock(gMu);
		// 这里写线程逻辑代码
		cout << "线程id: " << GetCurrentThreadId() << " 这里写线程逻辑代码 Run1::run" << endl;
	};
	
	void release(){};
}; 

void ThreadFun()
{
	new WorkerThreadPool;
	for (size_t i = 0; i < 10; ++i)
	{
		WorkerThreadPool::getInstancePtr()->add(new Run1);
	}
}

void EndF()
{
	HANDLE hEvent = ::CreateEvent(NULL, FALSE, FALSE, L"EndF");
	::WaitForSingleObject(hEvent, INFINITE);
	::CloseHandle(hEvent);
}


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "start..." << endl;
	ThreadFun();
	EndF();
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值