关于线程池的问题,请有心人留步

sxbwjl 2011-05-09 08:43:58
功能:创建一个线程池,打印 0--9
问题:总是打印不全,比如打印 0 2 1 3 5 6 7 少了4 8 9

代码在UNIX下编译通过,并可生产执行文件,请有心人帮我看看问题出在哪,若能修改后能完整打印0--9,高分结贴
threadPool.h:

#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
#include <signal.h>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#define BUSY_THRESHOLD 0.5
#define MANAGE_INTERVAL 2


class ThreadWorkFuncParam
{
public:
int ts;
ThreadWorkFuncParam()
{
ts=0;
}
};


typedef void (*threadWorkFunc)(ThreadWorkFuncParam ¶m) ;
void test(ThreadWorkFuncParam ¶m);
threadWorkFunc twf=test;





void* threadPoolFunc(void* p);


void* threadManageFunc(void* p);

class ThreadInfo
{

public:
pthread_t thread_id;
ThreadWorkFuncParam job;
int isBusy;
pthread_mutex_t busyLock;

pthread_cond_t threadCond;
pthread_mutex_t threadLock;


int isFlish;
ThreadInfo()
{
isBusy=FALSE;
isFlish=0;
pthread_cond_init(&threadCond,NULL);
pthread_mutex_init(&threadLock,NULL);

pthread_mutex_init(&busyLock,NULL);
}
~ThreadInfo()
{
pthread_mutex_destroy(&threadLock);
pthread_mutex_destroy(&busyLock);
pthread_cond_destroy(&threadCond);

}
void callThreadWorkFunc(threadWorkFunc &p)
{
p(job);
}

};
vector<ThreadInfo> threadInfoList;
class ThreadPool
{
public:
int tmFlish;
int threadMinNum;
int threadCurNum;
int threadMaxNum;
pthread_t threadManageId;
pthread_mutex_t threadPoolMutex;

pthread_mutex_t deleteMutex;
pthread_cond_t deleteCond;


ThreadPool()
{
tmFlish=0;
threadInfoList.clear();
pthread_mutex_init(&threadPoolMutex,NULL);
pthread_mutex_init(&deleteMutex,NULL);
pthread_cond_init(&deleteCond,NULL);

}
ThreadPool(int minNum,int maxNum)
{
cout<<"begin ThreadPool:ThreadPool(int minNum,int maxNum)..."<<endl;
threadMinNum=minNum;
threadMaxNum=maxNum;
threadCurNum=threadMinNum;
threadInfoList.clear();
pthread_mutex_init(&threadPoolMutex,NULL);
pthread_mutex_init(&deleteMutex,NULL);
pthread_cond_init(&deleteCond,NULL);

//!生成线程信息
for(int i=0;i<maxNum;i++)
{
ThreadInfo ti;
ti.isBusy=FALSE;
threadInfoList.push_back(ti);
}
cout<<"end ThreadPool:ThreadPool(int minNum,int maxNum)..."<<endl;
}
~ThreadPool()
{
//pthread_mutex_destroy(&threadPoolMutex);
}
int init();


int close();


int processWork(ThreadWorkFuncParam &job);


int getNoByThreadId(pthread_t &id);


int addThread(ThreadWorkFuncParam &job);


int deleteThread();


float getStatus();

};



#endif

threadPool.cpp:


#include "threadPool.h"


int ThreadPool::init()
{
int err=0;
int fi=0;
for(;fi<threadMinNum;fi++)
{
err = pthread_create(&(threadInfoList[fi].thread_id), NULL, threadPoolFunc, this);
if(0 != err){
return FALSE;
}

}
err = pthread_create(&threadManageId, NULL, threadManageFunc, this);
if(0 != err){
cout<<"init: creat manage thread failed... \n"<<endl;
return FALSE;
}
return TRUE;
}

int ThreadPool::close()
{

for(int i=0;i<threadCurNum;i++)
{
//cout<<i<<" "<<threadInfoList[i].job.ts<<" "<<threadInfoList[i].isBusy<<endl;
if(threadInfoList[i].isBusy== false)
{
threadInfoList[i].isFlish=1;
pthread_cond_signal(&(threadInfoList[i].threadCond));
}else
{
//sleep(1);
}
}

tmFlish=1;
pthread_mutex_destroy(&threadPoolMutex);
return TRUE;
}


int ThreadPool::processWork(ThreadWorkFuncParam &njob)
{
int i=0;
for(;i<threadCurNum;i++)
{
pthread_mutex_lock(&(threadInfoList[i].busyLock));

if(!(threadInfoList[i].isBusy))
{
threadInfoList[i].isBusy=TRUE;
threadInfoList[i].job=njob;
pthread_mutex_unlock(&(threadInfoList[i].busyLock));
pthread_cond_signal(&(threadInfoList[i].threadCond));
return TRUE;
}

pthread_mutex_unlock(&(threadInfoList[i].busyLock));
}


if(threadCurNum<threadMaxNum)
{
pthread_mutex_lock(&threadPoolMutex);
if(addThread(njob))
{

i=threadCurNum-1;

}else
{
pthread_mutex_unlock(&threadPoolMutex);
return FALSE;
}
pthread_mutex_unlock(&threadPoolMutex);
pthread_cond_signal(&(threadInfoList[i].threadCond));
return TRUE;
}else
{
cout<<"ThreadPool: tp thread already max ,can't create thread again!...\n"<<endl;
}
return FALSE;
}

int ThreadPool::getNoByThreadId(pthread_t &id)
{
for(int i=0;i<threadCurNum;i++)
{
if(threadInfoList[i].thread_id == id)
{
//cout<<"find thread "<<id<<" the list sort is "<<i<<endl;
return i;
}
}
return -1;
}

int ThreadPool::addThread(ThreadWorkFuncParam &njob)
{
if(threadMaxNum <= threadCurNum)
{
return FALSE;
}

int err=0;
err = pthread_create(&(threadInfoList[threadCurNum].thread_id), NULL, threadPoolFunc, this);
if(0 != err)
{
cout<<"addThread: add work thread failed... \n"<<endl;
return FALSE;
}
pthread_mutex_lock(&(threadInfoList[threadCurNum].busyLock));
threadInfoList[threadCurNum].job=njob;
threadInfoList[threadCurNum].isBusy=1;
threadCurNum=threadCurNum+1;
pthread_mutex_unlock(&(threadInfoList[threadCurNum].busyLock));

return TRUE;
}

int ThreadPool::deleteThread()
{
if(threadCurNum <= threadMinNum)
{
return false;
}
int temp=threadCurNum;
for(int i=0;i<threadCurNum;i++)
{
if(threadInfoList[i].isBusy == FALSE)
{
threadInfoList[i].isFlish=1;
pthread_cond_signal(&(threadInfoList[i].threadCond));
pthread_mutex_lock(&deleteMutex);
pthread_cond_wait(&deleteCond, &deleteMutex);
threadCurNum=threadCurNum-1;
pthread_mutex_unlock(&deleteMutex);
//cout<<"after delete size is "<<threadInfoList.size()<<endl;

return TRUE;
}
}
if(temp == threadCurNum)
{
cout<<"deleteThread: no idle thread to delete... \n"<<endl;
return FALSE;
}
return TRUE;
}

float ThreadPool::getStatus()
{
int busyNum=0;
for(int i=0;i<threadCurNum;i++)
{
if(threadInfoList[i].isBusy == TRUE)
{
busyNum++;
}
}
//cout<<"getStatus:the status is: "<<busyNum/threadCurNum<<endl;
return busyNum/threadCurNum;
}

void* threadPoolFunc(void *p)
{
ThreadPool *tp=(ThreadPool *)p;
pthread_t curid = pthread_self();

int i=0;
while(true)
{
i=tp->getNoByThreadId(curid);
pthread_mutex_lock(&(threadInfoList[i].threadLock));
pthread_cond_wait(&(threadInfoList[i].threadCond), &(threadInfoList[i].threadLock));
pthread_mutex_unlock(&(threadInfoList[i].threadLock));
if(1 == threadInfoList[i].isFlish)
{
//cout<<"thread : "<<curid <<" is exit ...\n"<<endl;
pthread_cond_signal(&(tp->deleteCond));
pthread_exit("accept stop command...");
break;
}
threadInfoList[i].callThreadWorkFunc(twf);

pthread_mutex_lock(&(threadInfoList[i].busyLock));
threadInfoList[i].isBusy=FALSE;
pthread_mutex_unlock(&(threadInfoList[i].busyLock));
}
return NULL;

}

void* threadManageFunc(void *p)
{

sleep(MANAGE_INTERVAL);
ThreadPool *tp=(ThreadPool *)p;
do{
if(1==tp->tmFlish) break;
if(tp->getStatus() < BUSY_THRESHOLD)
{
//tp->deleteThread();
}
sleep(MANAGE_INTERVAL);
}while(TRUE);
return NULL;
}

void test(ThreadWorkFuncParam ¶m)
{
cout<<param.ts<<endl;
}
int main(int argv,char **argc)
{
ThreadPool tp(4,10);
tp.init();
ThreadWorkFuncParam twfp;
for(int i=0;i<10;i++)
{
ThreadWorkFuncParam twfp;
twfp.ts=i;
tp.processWork(twfp);
//sleep(1);
}


tp.close();
pause();

return 0;
}


...全文
44 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
李亚超 2011-05-09
  • 打赏
  • 举报
回复
线程同步,在修改数据之前,加锁
MSOKD 2011-05-09
  • 打赏
  • 举报
回复
多线程要注意同步问题 

69,380

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧