c++线程池简单实现
#pragma once
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
#include<vector>
#include<cstring>
#include<algorithm>
#include<string>
#include<Windows.h>
#include<time.h>
using namespace std;
class CThreadPool {
public:
class CThreadItem {
public:
bool is_running;
thread mythread;
CThreadPool* p_This;
//构造函数
CThreadItem(CThreadPool* pthis) :p_This(pthis), is_running(false) {
cout << this << endl;
}
//析构函数
~CThreadItem() {}
};
static bool m_shutdown; //线程退出标志,false不退出,true退出
int m_iThreadNum;
CThreadPool();
~CThreadPool();
void Stopall();
void Call();
void Creat(int);
void* ThreadFun(void*);
void inMsgRecvQueueAndSignal(vector<int> resh, int nImageWidth, int nImageHeight, int start, int end);
bool a = false;
int b = 0;
//储存数据
vector<vector<int>> m_msg;
vector<vector<int>> msg;
vector<int> Width;
vector<int> Height;
vector<int> Start;
vector<int> End;
vector<vector<int>> index;
///
vector<CThreadItem*> m_threadVec;
std::mutex m_pthreadMutex;
condition_variable m_pthreadCond;
};
bool CThreadPool::m_shutdown = false;
CThreadPool::CThreadPool() {
}
CThreadPool::~CThreadPool() {
lblfor1:
for (auto iter = m_msg.begin(); iter != m_msg.end(); ++iter) {
vector<int> buf = *iter;
delete &buf;
goto lblfor1;
}
}
void CThreadPool::Call() {
m_pthreadCond.notify_one();
}
//停止工作线程
void CThreadPool::Stopall() {
if (m_shutdown) {
return;
}
m_shutdown = true;
m_pthreadCond.notify_all();
for (auto iter = m_threadVec.begin(); iter != m_threadVec.end(); iter++)
{
(*iter)->mythread.join();
}
cout << "------\n";
for (auto iter = m_threadVec.begin(); iter != m_threadVec.end(); iter++)
{
CThreadItem* tmp = *iter;
cout << tmp << endl;
delete tmp;
}
cout << "------\n";
m_threadVec.clear();
printf("cthreadpool::stopall()成功返回,线程池中线程全部正常结束!\n");
return;
}
//创建线程池
void CThreadPool::Creat(int num)
{
//工作线程个数
m_iThreadNum = num;
CThreadItem* pNew;
cout << "------\n";
//创建工作线程
for (int i = 0; i < m_iThreadNum; ++i)
{
pNew = new CThreadItem(this);
pNew->mythread = thread(&CThreadPool::ThreadFun, this, pNew);
m_threadVec.push_back(pNew);
}
cout << "------\n";
lblfor:
for (auto iter = m_threadVec.begin(); iter != m_threadVec.end(); ++iter)
{
if (!((*iter)->is_running))
{
cout << "iter:" << *iter << endl;
goto lblfor;
}
}
cout << " Creat ok\n";
}
//工作线程
void* CThreadPool::ThreadFun(void* Data)
{
CThreadItem* pThread = static_cast<CThreadItem*>(Data);
CThreadPool* pool = pThread->p_This;
//std::mutex m;
while (1)
{
std::unique_lock<std::mutex> lck(m_pthreadMutex);
//判断所有工作线程是否完全结束工作
if (b == 10) {
b = 0;
a = true;
A.push_back(msg.size());
msg.clear();
}
else {
a = false;
}
//让每个线程轮流取数据
while (m_msg.size() == 0 && m_shutdown == false)
{
if (!pThread->is_running) {
pThread->is_running = true;
}
m_pthreadCond.wait(lck);
}
if (m_shutdown)
{
lck.unlock();
return (void*)0;
}
vector<int> jobbuf = m_msg.front();
m_msg.erase(m_msg.begin());
cout << "get:" << jobbuf[0] + 10 << endl;
b = b + 1;
msg.push_back({ jobbuf[0] + 10,jobbuf[1] });
lck.unlock();
}
return (void*)0;
}
//加载数据
void CThreadPool::inMsgRecvQueueAndSignal(vector<int> rhs, int nImageWidth, int nImageHeight, int start, int end)
{
m_pthreadMutex.lock();
m_msg.push_back(rhs);
Width.push_back(nImageWidth);
Height.push_back(nImageHeight);
Start.push_back(start);
End.push_back(end);
m_pthreadMutex.unlock();
Call();
}
int main()
{
// 检测是否有内存泄漏
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
CThreadPool* pCThreadPool = new CThreadPool();
pCThreadPool->Creat(4);
vector<vector<int>> Mess = { {10,10},{11,11},{12,12},{13,13},{14,14},{15,15},{16,16},{17,17},{18,18},{19,19} };
std::mutex lck;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; ++i)
{
vector<int> mess;
mess = Mess[i];
pCThreadPool->inMsgRecvQueueAndSignal(mess,1,1,1,1);
}
while (1) {
lck.lock();
if (A.size()!=0) {
cout << "hello" << A[0] << endl;
A.erase(A.begin());
lck.unlock();
Sleep(100);
//pCThreadPool->m_pthreadCond.wait(lck1);
//pCThreadPool->m_pthreadCond.notify_all();
//lck.unlock();
//cout <<"hello" << pCThreadPool->a<< endl;
break;
}
lck.unlock();
}
}
Sleep(5000);
pCThreadPool->Stopall();
delete pCThreadPool;
getchar();
return 0;
}