创建线程池,比如包括n个线程,然后自己可以增加任务队列,创建的线程从队列中取任务执行,执行完后,取下一个任务。
如果任务是死循环,这这个线程就会一直执行这个任务。
threadpool.h
Code
#include <list>
#include <windows.h>
/* Asynchronized Queue */
//异步队列
template <class T>
class AsynchQueue
{
public:
AsynchQueue();
~AsynchQueue();
void Enqueue(T* data);//入队
T* Dequeue();//出队
private:
std::list<T*> queue;
HANDLE hMutex;
HANDLE hEvent;
AsynchQueue(const AsynchQueue<T>& q);
AsynchQueue &operator=(const AsynchQueue<T>&);
};
/* Job that passed to the thread */
class Job
{
public:
virtual void Process() = 0;
virtual ~Job() {};
};
/* Thread Pool */
class ThreadPool
{
public:
ThreadPool(int num, BOOL releaseJob);
~ThreadPool();
void Assignment(Job *job);
void Work();
BOOL IsStopping();
private:
ThreadPool(const ThreadPool &);
ThreadPool &operator=(const ThreadPool &);
int m_iThread;
AsynchQueue<Job> queue;
std::list<HANDLE> threads;
HANDLE hMutex;
BOOL stopMe;
BOOL m_releaseJob;
};
#include <list>
#include <windows.h>
/* Asynchronized Queue */
//异步队列
template <class T>
class AsynchQueue
{
public:
AsynchQueue();
~AsynchQueue();
void Enqueue(T* data);//入队
T* Dequeue();//出队
private:
std::list<T*> queue;
HANDLE hMutex;
HANDLE hEvent;
AsynchQueue(const AsynchQueue<T>& q);
AsynchQueue &operator=(const AsynchQueue<T>&);
};
/* Job that passed to the thread */
class Job
{
public:
virtual void Process() = 0;
virtual ~Job() {};
};
/* Thread Pool */
class ThreadPool
{
public:
ThreadPool(int num, BOOL releaseJob);
~ThreadPool();
void Assignment(Job *job);
void Work();
BOOL IsStopping();
private:
ThreadPool(const ThreadPool &);
ThreadPool &operator=(const ThreadPool &);
int m_iThread;
AsynchQueue<Job> queue;
std::list<HANDLE> threads;
HANDLE hMutex;
BOOL stopMe;
BOOL m_releaseJob;
};
main.cpp
Code
#include <iostream>
#include <tchar.h>
#include "threadpool.h"
using namespace std;
class MyJob1 : public Job
{
public:
MyJob1(int index) : data(index) {};
virtual void Process()
{
cout << data << endl;//重载job函数,编写自己的线程函数
}
~MyJob1() {};
private:
int data;
};
class MyJob2 : public Job
{
public:
MyJob2(int index) : data(index+1000) {};
virtual void Process()
{
cout << data << endl;
}
~MyJob2() {};
private:
int data;
};
class MyJob3 : public Job
{
public:
MyJob3(char index) : data(index) {};
virtual void Process()
{
cout << data << endl;
}
~MyJob3() {};
private:
char data;
};
int main(int argc, char **argv)
{
ThreadPool tp(4, true);
for (int i=0; i<10; i++)
{
Job *job = new MyJob1(i);
tp.Assignment(job);
job = new MyJob2(i);
tp.Assignment(job);
job = new MyJob3('a');
tp.Assignment(job);
Sleep(5);
}
return 0;
}
#include <iostream>
#include <tchar.h>
#include "threadpool.h"
using namespace std;
class MyJob1 : public Job
{
public:
MyJob1(int index) : data(index) {};
virtual void Process()
{
cout << data << endl;//重载job函数,编写自己的线程函数
}
~MyJob1() {};
private:
int data;
};
class MyJob2 : public Job
{
public:
MyJob2(int index) : data(index+1000) {};
virtual void Process()
{
cout << data << endl;
}
~MyJob2() {};
private:
int data;
};
class MyJob3 : public Job
{
public:
MyJob3(char index) : data(index) {};
virtual void Process()
{
cout << data << endl;
}
~MyJob3() {};
private:
char data;
};
int main(int argc, char **argv)
{
ThreadPool tp(4, true);
for (int i=0; i<10; i++)
{
Job *job = new MyJob1(i);
tp.Assignment(job);
job = new MyJob2(i);
tp.Assignment(job);
job = new MyJob3('a');
tp.Assignment(job);
Sleep(5);
}
return 0;
}
threadpool.cpp
Code
#include "threadpool.h"
template <class T>
AsynchQueue<T>::AsynchQueue()
{
hMutex = CreateMutex(NULL, FALSE, NULL);//初始值为false,调用者没有得到使用权,要先等待其为false
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);//第二个参数为false,自动复位
}
template <class T>
AsynchQueue<T>::~AsynchQueue()
{
//关闭句柄
CloseHandle(hMutex);
CloseHandle(hEvent);
}
template <class T>
void AsynchQueue<T>::Enqueue(T* data)
{
WaitForSingleObject(hMutex, INFINITE);
queue.push_back(data);
ReleaseMutex(hMutex);
SetEvent(hEvent);
}
template <class T>
T *AsynchQueue<T>::Dequeue()
{
WaitForSingleObject(hEvent, INFINITE);
WaitForSingleObject(hMutex, INFINITE);
T* data = queue.front();
queue.pop_front();
if (queue.size() > 0) SetEvent(hEvent);
ReleaseMutex(hMutex);
return data;
}
DWORD WINAPI ThreadFunc(LPVOID lpParam);
ThreadPool::ThreadPool(int num, BOOL releaseJob) :
m_iThread(num), m_releaseJob(releaseJob)
{
for (int i=0; i<num; i++)
{
DWORD dwThreadId;
HANDLE hThread;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
this, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
threads.push_back(hThread); //句柄入栈,threads句柄list
}
hMutex = CreateMutex(NULL, TRUE, NULL);
stopMe = FALSE;
ReleaseMutex(hMutex);
}
ThreadPool::~ThreadPool()
{
WaitForSingleObject(hMutex, INFINITE);
stopMe = TRUE;
ReleaseMutex(hMutex);
HANDLE *handles = new HANDLE[m_iThread];
int i=0;
std::list<HANDLE>::iterator iter;
for (iter = threads.begin();
iter != threads.end();
iter ++) {
handles[i++] = *iter;
}
WaitForMultipleObjects(m_iThread, handles, TRUE, 2000);//等待所有线程终止
for (iter = threads.begin();
iter != threads.end();
iter ++) {
CloseHandle(*iter);
}
delete [] handles;
CloseHandle(hMutex);
}
BOOL ThreadPool::IsStopping()
{
WaitForSingleObject(hMutex, INFINITE);
BOOL stop = stopMe;//线程是否停止运行
ReleaseMutex(hMutex);
return stop;
}
void ThreadPool::Assignment(Job *job)
{
queue.Enqueue(job);//线程运行函数入栈
}
void ThreadPool::Work()
{
Job *job = queue.Dequeue();
job->Process();
if (m_releaseJob)
delete job;
};
//线程函数参数为线程池指针
DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
ThreadPool *pool = static_cast<ThreadPool *>(lpParam);
while (!pool->IsStopping())
{
pool->Work();
}
return 0;
}
#include "threadpool.h"
template <class T>
AsynchQueue<T>::AsynchQueue()
{
hMutex = CreateMutex(NULL, FALSE, NULL);//初始值为false,调用者没有得到使用权,要先等待其为false
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);//第二个参数为false,自动复位
}
template <class T>
AsynchQueue<T>::~AsynchQueue()
{
//关闭句柄
CloseHandle(hMutex);
CloseHandle(hEvent);
}
template <class T>
void AsynchQueue<T>::Enqueue(T* data)
{
WaitForSingleObject(hMutex, INFINITE);
queue.push_back(data);
ReleaseMutex(hMutex);
SetEvent(hEvent);
}
template <class T>
T *AsynchQueue<T>::Dequeue()
{
WaitForSingleObject(hEvent, INFINITE);
WaitForSingleObject(hMutex, INFINITE);
T* data = queue.front();
queue.pop_front();
if (queue.size() > 0) SetEvent(hEvent);
ReleaseMutex(hMutex);
return data;
}
DWORD WINAPI ThreadFunc(LPVOID lpParam);
ThreadPool::ThreadPool(int num, BOOL releaseJob) :
m_iThread(num), m_releaseJob(releaseJob)
{
for (int i=0; i<num; i++)
{
DWORD dwThreadId;
HANDLE hThread;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
this, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
threads.push_back(hThread); //句柄入栈,threads句柄list
}
hMutex = CreateMutex(NULL, TRUE, NULL);
stopMe = FALSE;
ReleaseMutex(hMutex);
}
ThreadPool::~ThreadPool()
{
WaitForSingleObject(hMutex, INFINITE);
stopMe = TRUE;
ReleaseMutex(hMutex);
HANDLE *handles = new HANDLE[m_iThread];
int i=0;
std::list<HANDLE>::iterator iter;
for (iter = threads.begin();
iter != threads.end();
iter ++) {
handles[i++] = *iter;
}
WaitForMultipleObjects(m_iThread, handles, TRUE, 2000);//等待所有线程终止
for (iter = threads.begin();
iter != threads.end();
iter ++) {
CloseHandle(*iter);
}
delete [] handles;
CloseHandle(hMutex);
}
BOOL ThreadPool::IsStopping()
{
WaitForSingleObject(hMutex, INFINITE);
BOOL stop = stopMe;//线程是否停止运行
ReleaseMutex(hMutex);
return stop;
}
void ThreadPool::Assignment(Job *job)
{
queue.Enqueue(job);//线程运行函数入栈
}
void ThreadPool::Work()
{
Job *job = queue.Dequeue();
job->Process();
if (m_releaseJob)
delete job;
};
//线程函数参数为线程池指针
DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
ThreadPool *pool = static_cast<ThreadPool *>(lpParam);
while (!pool->IsStopping())
{
pool->Work();
}
return 0;
}